Search code examples
rfunctionvectorunique

How to code to get an output vector list of unique elements based satisfying two conditions?


I'm trying to get list of uniques elements based on conditions of two columns in R.

For example, I have 4 groups and I want to get unique list of names of participants who are in group-1.

This requires to specify the two conditions in the code:

Unique(df$participants XXX_group_XXX).

How to code this condition specifically to get the output vecort list satisfying both conditions?


Solution

  • A simple solution using only base R:

    set.seed(7*11*13)
    
    name <- sample(LETTERS[1:10], 100, replace=TRUE)
    G <- sample(1:5, 100, replace=TRUE)
     
    U <- tapply(name, G, unique) 
    
    > U
    $`1`
    [1] "G" "F" "D" "B" "J" "A" "E" "H" "C"
    
    $`2`
    [1] "C" "J" "D" "B" "F" "G"
    
    $`3`
    [1] "C" "G" "H" "D" "F" "E" "I" "B" "J"
    
    $`4`
    [1] "F" "B" "G" "E" "I" "C" "H" "D" "J"
    
    $`5`
    [1] "G" "D" "A" "H" "F" "E" "B" "J" "C"