Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R- how to conditionally remove first row of group_by

Tags:

r

dplyr

I need to conditionally remove the first row of a group. I want to group by column gr, then remove the first row of each group only if the first row of the group has value a

e.g.

gr  value
1   b
1   c
1   a
2   a
2   d
3   b
3   a
3   h
3   a
4   a
4   a
4   g

Would become:

gr  value
1   b
1   c
1   a
2   d
3   b
3   a
3   h
3   a
4   a
4   g

I know how to remove first row of groups:

library(dplyr)
df <- df %>% group_by(a) %>% slice(2:n()) %>% ungroup()

But I have no idea how to add the condition to only do this if of this first row, df$value = a

I'm new to R, it's still rather complicated to me, and I cannot find an answer to this problem anywhere.

Thank you very much!

like image 269
Isjitar Avatar asked Jun 11 '18 09:06

Isjitar


1 Answers

Apply your conditions within filter statement

library(dplyr)

df %>%
  group_by(gr) %>%
  filter(!(value == 'a' & row_number() == 1))


Sample data:

df <- structure(list(gr = c(1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 
4L, 4L), value = c("b", "c", "a", "a", "d", "b", "a", "h", "a", 
"a", "a", "g")), .Names = c("gr", "value"), class = "data.frame", row.names = c(NA, 
-12L))

Update: or as @akrun has suggested

library(dplyr)

df %>% 
  group_by(gr) %>% 
  slice(which(!(value == 'a' & row_number() == 1)))
like image 97
1.618 Avatar answered Sep 21 '22 10:09

1.618