我正在使用emmeans
包,但不明白为什么它能够工作
library(emmeans)
mod <- lm(mpg ~ disp + hp, data = mtcars)
l1 <- emmeans(mod, list("disp","hp"))
for (x in l1) {print(data.frame(x))}
disp emmean SE df lower.CL upper.CL
1 230.7219 20.09062 0.5527102 29 18.96021 21.22104
hp emmean SE df lower.CL upper.CL
1 146.6875 20.09062 0.5527102 29 18.96021 21.22104
但这个不行吗?
lapply(l1, function(x) data.frame(x))
Error in as.data.frame.default(x[[i]], optional = TRUE) :
cannot coerce class ‘"function"’ to a data.frame
使用普通列表,我可以得到预期的输出(列表中的数据框):
l2 <- list(a=matrix(c(1,2,3,4)), b=matrix(c(5,6,7,8)))
lapply(l2, function(x) data.frame(x))
$a
x
1 1
2 2
3 1
4 3
$b
x
1 4
2 5
3 4
4 6
?emmeans
中,当specs
是一个列表时,你会得到一个?emm_list
对象,它有一个as.data.frame
方法,因此你可以执行as.data.frame(l1)
但它会产生警告。 - rawrMap(data.frame, l1)
,它就能正常工作。看起来值得提交一个错误报告。 - Ritchie Sacramentolapply(l1, \(x) data.frame(as.emmGrid(x)))
也是一个选项。 - Ritchie Sacramento