我试图用来mapply应用t.test两个参数列表。第一个列表formulas包含三个公式,第二个列表periods包含该子集的三个向量,my.data我将其与MoreArgs参数一起传递。
我可以t.test使用循环手动执行 s for(也在下面),但我不知道为什么我的mapply使用失败了。这不是正确的使用时间mapply吗?
# similar data
my.data <- data.frame(CAR1=rnorm(150),
                      CAR2=rnorm(150),
                      CAR3=rnorm(150),
                      period=rep(1:3, each=50),
                      treated=rep(1:2, times=75)
                      )
# two lists to pass as arguments to `t.test()`
# `subset`
periods <- list(my.data$period == 1,
                my.data$period <= 2,
                my.data$period <= 3
                )
# `formula`
formulas <- list(CAR1 ~ treated,
                 CAR2 ~ treated,
                 CAR3 ~ treated
                 )
# manual solution works
ttests <- list()
for (i in 1:3) {
    ttests[[i]] <- t.test(formulas[[i]], 
                          data=my.data, 
                          subset=periods[[i]]
                          )
}
# but `mapply` fails
ttest <- mapply(FUN=t.test, 
                formula=formulas, 
                subset=periods, 
                MoreArgs=list(data=my.data),
                SIMPLIFY=FALSE
                )
# with error "Error in eval(expr, envir, enclos) : object 'dots' not found"