rdataframemergedata-manipulation

Create a new variable based on non-existing values on another variable in R dataframe


I know the title sounds very basic but I didn't find a solution by searching.

I have a dataframe like this where id refers to a participant:

id  condition
1   0 
1   0
1   1
1   2
2   0
2   0
2   1 
3   0

Now, some participants have failed to provide any observations for condition "2", and I'd like to create a new variable indicating whether the participant has any "2" responses vs. not. So the result should be

id  condition  cond2
1   0          yes
1   0          yes
1   1          yes
1   2          yes
2   0          no
2   0          no
2   1          no
3   0          no

is there an easy way for this? Thank you in advance!


Solution

  • Data

    df <- structure(list(id = c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 3L), condition = c(0L, 
    0L, 1L, 2L, 0L, 0L, 1L, 0L)), class = "data.frame", row.names = c(NA, 
    -8L))
    

    Tidyverse

    library(dplyr)
    df %>%
      mutate(cond2 = if_else(any(condition == 2), "yes", "no"), .by = id)
    
    #   id condition cond2
    # 1  1         0   yes
    # 2  1         0   yes
    # 3  1         1   yes
    # 4  1         2   yes
    # 5  2         0    no
    # 6  2         0    no
    # 7  2         1    no
    # 8  3         0    no
    

    Base R

    df$cond2 <- ave(df$condition, df$id, FUN = function(x) ifelse(any(x == 2), "yes", "no"))
    
    df
    #   id condition cond2
    # 1  1         0   yes
    # 2  1         0   yes
    # 3  1         1   yes
    # 4  1         2   yes
    # 5  2         0    no
    # 6  2         0    no
    # 7  2         1    no
    # 8  3         0    no