I have a grouped data_frame with a tag column accepting the values ββ"0" and "1". In each group I need to find the first occurrence of "1" and change all other occurrences to "0". Is there any way to achieve this in dplyr?
For example, let's take the βirisβ data and add an extra βtagβ column:
data(iris) set.seed(1) iris$tag <- sample( c(0, 1), 150, replace = TRUE, prob = c(0.8, 0.2)) giris <- iris %>% group_by(Species)
In "giris" in the group "setosa" I need to save only the first occurrence of "1" (ie in the 4th line) and set the remaining "0". It is like applying a mask or something else ...
Is there any way to do this? I experimented with βoneβ and βduplicatedβ, but I did not succeed. I thought about filtering only β1β, saving them and then joining the remaining set, but this seems inconvenient, especially for a 12 GB data set.
Thanks in advance!
r dplyr which
rpl
source share