使用 dplyr 和 add_row() 在每个组中添加行

问题描述 投票:0回答:3

如果我使用以下命令向

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
r dataframe dplyr row tibble
3个回答
28
投票

更新的版本将使用

group_modify()
而不是
do()

iris %>%
  as_tibble() %>%
  group_by(Species) %>% 
  group_modify(~ add_row(.x,.before=0))
#> # A tibble: 153 x 5
#> # Groups:   Species [3]
#>    Species Sepal.Length Sepal.Width Petal.Length Petal.Width
#>    <fct>          <dbl>       <dbl>        <dbl>       <dbl>
#>  1 setosa          NA          NA           NA          NA  
#>  2 setosa           5.1         3.5          1.4         0.2
#>  3 setosa           4.9         3            1.4         0.2

20
投票

如果你想使用分组操作,你需要

do
就像 JasonWang 在他的评论中描述的那样,因为其他函数如
mutate
summarise
期望结果与分组数据帧具有相同的行数(在你的案例,50)或一行(例如总结时)。

您可能知道,一般来说,

do
可能会很慢,如果您无法通过其他方式实现结果,则应该是最后的手段。您的任务非常简单,因为它只涉及在数据框中添加额外的行,这可以通过简单的索引来完成,例如查看
iris[NA, ]
的输出。

你想要的本质上是创建一个向量

indices <- c(NA, 1:50, NA, 51:100, NA, 101:150)

(因为第一组位于第 1 至 50 行,第二组位于第 51 至 100 行,第三组位于第 101 至 150 行)。

结果就是

iris[indices, ]

构建此向量的更通用方法是使用

group_indices

indices <- seq(nrow(iris)) %>% 
    split(group_indices(iris, Species)) %>% 
    map(~c(NA, .x)) %>%
    unlist

map
来自
purrr
,我假设你已经加载了,因为你已经用
tidyverse
标记了它)。


5
投票

稍有不同,也可以这样做:

library(purrr)
library(tibble)

iris %>%
  group_split(Species) %>%
  map_dfr(~ .x %>%
            add_row(.before = 1))

# A tibble: 153 x 5
   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
          <dbl>       <dbl>        <dbl>       <dbl> <fct>  
 1         NA          NA           NA          NA   NA     
 2          5.1         3.5          1.4         0.2 setosa 
 3          4.9         3            1.4         0.2 setosa 
 4          4.7         3.2          1.3         0.2 setosa 
 5          4.6         3.1          1.5         0.2 setosa 
 6          5           3.6          1.4         0.2 setosa 
 7          5.4         3.9          1.7         0.4 setosa 
 8          4.6         3.4          1.4         0.3 setosa 
 9          5           3.4          1.5         0.2 setosa 
10          4.4         2.9          1.4         0.2 setosa 
# ... with 143 more rows

这也可以用于分组数据框,但是,它有点冗长:

library(dplyr)

iris %>%
  group_by(Species) %>%
  summarise(Sepal.Length = c(NA, Sepal.Length), 
            Sepal.Width = c(NA, Sepal.Width), 
            Petal.Length = c(NA, Petal.Length),
            Petal.Width = c(NA, Petal.Width), 
            Species = c(NA, Species))
© www.soinside.com 2019 - 2024. All rights reserved.