看来,对于存储在列表列中的某些类型的对象, dplyr::mutate
只会对它们起作用一次 . 也就是说, mutate
的两次连续使用会导致错误 .
library(dplyr)
one_mod <- data.frame(grp = "a", x = runif(5,0,1)) %>%
tbl_df %>%
mutate(y = rnorm(x,x*2,1)) %>%
group_by(grp) %>%
do(mod = lm(y~x,data = .))
这种方法不起作用:
one_mod %>%
mutate(rsq = summary(mod)$r.squared) %>%
mutate(aic = AIC(mod))
# Error: unsupported type for column 'mod' (VECSXP)
但这个确实如此
one_mod %>%
mutate(rsq = summary(mod)$r.squared,
aic = AIC(mod))
#Source: local data frame [1 x 4]
#Groups: grp
#
# grp mod rsq aic
#1 a <S3:lm> 0.6615589 10.63317
1 回答
这已在this commit中修复 .
正如Hadley所暗示的,在两个
mutate
版本中,第一个没有创建rowwise_df
对象,这是我们保证每个组只涉及一行数据,因此我们可以引用mod
而不是mod[[1]]
. 除非我们知道数据可以被认为是rowwise_df
,否则我们不会使用mutate
处理列表列 .一切都很好,并将您的示例用作新的regression test