In principle, it seems if you have a working filter condition, then you have a working variable definition for a new column in df
. I would recommend using mutate
after group_by
, and then filtering on your new variable, as needed.
This will have advantages over merging the filtered results back into your data as it won't leave missing values for the rows that didn't pass the filter.
library(dplyr)
df <-
data.frame(Transitionyear = rep(2000:2004,
each = 4),
Dummy = 1:20)
df %>%
group_by(Transitionyear) %>%
mutate(Flag = row_number() == ceiling(n() / 2))
#> # A tibble: 20 x 3
#> # Groups: Transitionyear [5]
#> Transitionyear Dummy Flag
#> <int> <int> <lgl>
#> 1 2000 1 FALSE
#> 2 2000 2 TRUE
#> 3 2000 3 FALSE
#> 4 2000 4 FALSE
#> 5 2001 5 FALSE
#> 6 2001 6 TRUE
#> 7 2001 7 FALSE
#> 8 2001 8 FALSE
#> 9 2002 9 FALSE
#> 10 2002 10 TRUE
#> 11 2002 11 FALSE
#> 12 2002 12 FALSE
#> 13 2003 13 FALSE
#> 14 2003 14 TRUE
#> 15 2003 15 FALSE
#> 16 2003 16 FALSE
#> 17 2004 17 FALSE
#> 18 2004 18 TRUE
#> 19 2004 19 FALSE
#> 20 2004 20 FALSE