Search code examples
rrecode

R Recode variable for all observations that do not occur more than once


I have a simple dataframe that looks like the following:

Observation X1 X2 Group
1           2   4   1
2           6   3   2
3           8   4   2
4           1   3   3
5           2   8   4
6           7   5   5
7           2   4   5

How can I recode the group variable such that all non-recurrent observations are recoded as "unaffiliated"?

The desired output would be the following:

Observation X1 X2 Group
1           2   4   Unaffiliated
2           6   3   2
3           8   4   2
4           1   3   Unaffiliated
5           2   8   Unaffiliated
6           7   5   5
7           2   4   5


Solution

  • One way could be first grouping then checking for maximum of row number and finishing with an ifelse:

    library(dplyr)
    
    df %>% 
      group_by(Group) %>% 
      mutate(Group = ifelse(max(row_number()) == 1, "Unaffiliated", as.character(Group))) %>% 
      ungroup()
    
      Observation    X1    X2 Group       
            <int> <int> <int> <chr>       
    1           1     2     4 Unaffiliated
    2           2     6     3 2           
    3           3     8     4 2           
    4           4     1     3 Unaffiliated
    5           5     2     8 Unaffiliated
    6           6     7     5 5           
    7           7     2     4 5