【发布时间】:2017-09-10 05:25:46
【问题描述】:
如果我向 iris 数据集添加新行:
iris <- as_tibble(iris)
> iris %>%
add_row(.before=0)
# A tibble: 151 × 5
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
<dbl> <dbl> <dbl> <dbl> <chr>
1 NA NA NA NA <NA> <--- Good!
2 5.1 3.5 1.4 0.2 setosa
3 4.9 3.0 1.4 0.2 setosa
它有效。那么,为什么我不能在每个“子集”的顶部添加一个新行:
iris %>%
group_by(Species) %>%
add_row(.before=0)
Error: is.data.frame(df) is not TRUE
【问题讨论】:
-
升级您的
tibble版本,该错误消息至少为three months old。 (新的错误消息显示"Cannot add rows to grouped data frames",它回答了您关于它为什么不起作用的问题。) -
您可以使用
do向每个组添加行:iris %>% group_by(Species) %>% do(add_row(., .before=0))。 -
感谢 JasonWang 和 r2evans。我已经更新了我的包,使用 do() 就可以了。