我正在嘗試在功能DESIRED_OUTPUT內部實作我的以下foo()功能,但沒有成功。
有沒有辦法讓foo我的輸出完全匹配DESIRED_OUTPUT?
m="cyl hp wt vs gear
1 6.1875 146.6875 3.21725 0 3
2 6.1875 146.6875 3.21725 1 3
3 6.1875 146.6875 3.21725 0 4
4 6.1875 146.6875 3.21725 1 4
5 6.1875 146.6875 3.21725 0 5
6 6.1875 146.6875 3.21725 1 5"
dat <- read.table(text=m,h=T)
DESIRED_OUTPUT <- with(dat, paste(vs, gear))
# [1] "0 3" "1 3" "0 4" "1 4" "0 5" "1 5"
foo <- function(dat, nm){
with(dat, paste(nm))
}
# EXAMPLE OF USE:
foo(dat, c("vs","gear"))
uj5u.com熱心網友回復:
你可以使用
foo <- function(dat, nm) do.call(paste, dat[nm])
導致:
foo(dat, c("vs","gear"))
#> [1] "0 3" "1 3" "0 4" "1 4" "0 5" "1 5"
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/453428.html
