In the dataframe, if two rows have the same id then I want the new column status to read YES else NO
Here is my attempt
set.seed(111)
id <- c(1,1,2,2,3,4,5,6)
val <- c(9,0,2,4,1,0,0,2)
df <- data.frame(val,id)
df <- df%>%
group_by(id) %>%
mutate(status = ifelse(duplicated(id), 'YES', 'NO'))
val id status
1 9 1 NO
2 0 1 YES
3 2 2 NO
4 4 2 YES
5 1 3 NO
6 0 4 NO
7 0 5 NO
8 2 6 NO
I want the table to instead read:
val id status
1 9 1 YES
2 0 1 YES
3 2 2 YES
4 4 2 YES
5 1 3 NO
6 0 4 NO
7 0 5 NO
8 2 6 NO