Add a row to each group using dplyr and add_row ()

If I add a new row to the ìris with:

 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 

It works. So, why can't I add a new line on top of each "subset" with:

 iris %>% group_by(Species) %>% add_row(.before=0) Error: is.data.frame(df) is not TRUE 
+5
source share
1 answer

If you want to use a grouped operation, you need to do as JasonWang described in his comment, since other functions, such as mutate or summarise , expect results with the same number of rows as the grouped data frame (in your case, 50) or with one line (for example, when summing up).

As you probably know, in general, do can be slow and should be the last resort if you cannot achieve your result differently. Your task is quite simple, since it only involves adding additional rows to your data frame, which can be done by simple indexing, for example. look at the result of iris[NA, ] .

Do you want to create a vector

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

(since the first group is in lines from 1 to 50, the second is from 51 to 100, and the third is from 101 to 150).

The result is iris[indices, ] .

A more general way of constructing this vector is using group_indices .

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

( map comes from purrr , which I assume you loaded as you marked it with tidyverse ).

+3
source

Source: https://habr.com/ru/post/1266718/


All Articles