Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using group_by() similarly as filter() in dplyr?

Tags:

r

dplyr

On the sample data.frame:

df <- data.frame(V1 = c(1, 3, 4, NA, NA, 6, 9, NA, 10),
V2 = seq(1:9))

Using group_by() in the fashion of filter() gives these results:

df %>%
  group_by(miss = !is.na(V1)) %>%
  mutate(lag = V1 - lag(V1))

# A tibble: 9 x 4
# Groups:   miss [2]
     V1    V2 miss    lag
  <dbl> <dbl> <lgl> <dbl>
1    1.    1. TRUE    NA 
2    3.    2. TRUE     2.
3    4.    3. TRUE     1.
4   NA     4. FALSE   NA 
5   NA     5. FALSE   NA 
6    6.    6. TRUE     2.
7    9.    7. TRUE     3.
8   NA     8. FALSE   NA 
9   10.    9. TRUE     1.

It is exactly what I wanted but I'm curious whether it is intended to use group_by() also this way.

like image 484
tmfmnk Avatar asked Nov 08 '22 02:11

tmfmnk


1 Answers

For the sake of having a documented answer to this interesting question - from the comments, as @Joran has pointed out, group_by can be used with expressions. as @avid_useR pointed out, it does not really resemble filter, but more something like case_when, because no rows are removed.

like image 187
tjebo Avatar answered Nov 15 '22 06:11

tjebo