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!
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)))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With