【问题标题】:dplyr `mutate` returns error when used twicedplyr `mutate` 使用两次时返回错误
【发布时间】:2014-06-17 18:59:01
【问题描述】:

似乎,对于存​​储在列表列中的某些类型的对象,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

【问题讨论】:

  • 哦,嗯,我们可能会丢失 rowwise 属性。你能提交一个错误报告吗?

标签: r dplyr


【解决方案1】:

这已在this commit 中修复。

正如 Hadley 所暗示的,在两个mutate 版本中,第一个没有创建rowwise_df 对象,这是我们保证每个组只与一行数据相关,以便我们可以参考@ 987654325@ 而不是 mod[[1]]。除非我们知道可以将数据视为rowwise_df,否则我们不会处理带有mutate 的列表列。

现在一切顺利,并使用您的示例作为新的regression test

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))
# Source: local data frame [1 x 4]
# Groups: <by row>
# 
#   grp     mod        rsq      aic
# 1   a <S3:lm> 0.04744827 11.91253

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    • 2015-08-03
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多