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

Time:01-14

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?

CodePudding user response:

Would this work for you? I need to create a data frame first. Then I filter for the group you wish to see and get the unique values per group.

library(dplyr)
    
seed <- 123
# create some data    
data <- data.frame(
         name = sample(LETTERS, size = 100, replace = TRUE),
         group = sample(c(1, 2, 3, 4), size = 100, replace = TRUE)
    )
    
# base R
unique(data[data$group == 1, 1])
# or:
unique(data[data$group == 1, "name"])
    
# tidyverse
data %>%
    filter(group == 1) %>%
    distinct(name) %>%
    pull() # if you want a vector to be returned

CodePudding user response:

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"
  • Related