如果我添加了一个新行到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
版本,这个错误信息至少有三个月了(请参考链接https://github.com/tidyverse/tibble/blame/b32c2b952afdeff93d422512a132ec6d0a2e2fbc/R/add.R#L35-L37)。新的错误信息是“无法添加行到分组数据框”,这回答了你为什么不能工作的问题。 - r2evansdo
函数向每个组添加行:iris %>% group_by(Species) %>% do(add_row(., .before=0))
。 - JasonWang