I have filtered some rows of a data frame (df) based on some criteria using group_by and filter function of dplyr. Now I want to label those rows differently in the original dataframe (df). So that I can use if else function based on that label.
# the code tries to find out the middle index of the each group of Transitonyear.
df%>%
group_by(Transitionyear) %>%
filter(row_number()==ceiling(n()/2))
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.