Create new column based on occurence of at least one variable in other column by group

Consider the following data frame:

ID <- c(1,1,1,2,2,3,3,3,3)
A <- c("No","No","Yes","Yes","Yes","No","No","No","No")
B <- c("Yes","Yes","Yes","Yes","Yes","No","No","No","No")
df <- data.frame(ID,A,B)

I want to create column B, where the occurence of at least one "Yes" in column A results in only "Yes" values in column B for each separate ID. I have tried the two following approaches (I feel I am almost there):

library(dplyr)
df <- df %>% 
  group_by(ID) %>% 
  mutate(B1=ifelse(A == "Yes", "Yes", "No")) # B1 is the new column for comparison

unfortunately this gives the same column as A

and

df2 <- transform(df, B1= ave(A, ID, FUN=function(x) x[A == "Yes"]))

yields an error message:

1: In x[...] <- m : number of items to replace is not a multiple of replacement length

Help would be much appreciated.


Solution 1:

You almost had it. Here's a small edit to your pipe. Is this what you were after?

df <- df %>% 
 group_by(ID) %>% 
 mutate(B1=ifelse("Yes" %in% A, "Yes", "No"))
df