假设我有一个如下所示的数据框:
df1 <- data.frame("long name 1"=c(rnorm(n = 100, mean = 0, sd = 1),NA),
treatment=sample(x = c(0,1), size = 101, replace = T))
names(df1) <- c("long name 1", "treatment")
我想用5个参数创建一个函数。现在我有这个:
test.f <- function(data, vname, tname, tvalue=1, cvalue=0) {
vname<-as.name(vname)
tname <- as.name(tname)
mean.Xt <- mean(data$vname[data$tname==tvalue], na.rm = T)
}
当我运行它时,我收到以下错误:
test.f(data = df1, vname = "long name 1", tname = "treatment")
Warning message:
In mean.default(data$vname[data$tname == tvalue], na.rm = T) :
argument is not numeric or logical: returning NA
有没有办法做我想做的事?
答案 0 :(得分:5)
最好在函数中使用[[
代替$
来选择列。
test.f <- function(data, vname, tname, tvalue=1, cvalue=0) {
mean(data[[vname]][data[[tname]]==tvalue], na.rm = T)
}
test.f(data = df1, vname = "long name 1", tname = "treatment")
#[1] 0.1397585
与
相同mean(df1$`long name 1`[df1$treatment==1], na.rm=TRUE)
#[1] 0.1397585