4
I have this df:
structure(list(id = c("R054", "R054", "R054", "R054", "R054",
"GT68U", "GT68U", "GT68U", "GT68U", "GT68U", "G001", "G001",
"G001", "G001"), car1 = c("sim", "sim", "sim", "sim", "sim",
"sim", "nao", "sim", "nao", "nao", "nao", "nao", "nao", "nao"
)), row.names = c(NA, -14L), class = c("tbl_df", "tbl", "data.frame"
))
I would like to create a new column based on the values of car1 for the row set of each id. If the id always has car1 "yes" or "no" the new column only replicates the information. If the id has "yes" or "no" the new column should display "nd". So it would look like this:
id car1 output
R054 sim sim
R054 sim sim
R054 sim sim
R054 sim sim
R054 sim sim
GT68U sim nd
GT68U nao nd
GT68U sim nd
GT68U nao nd
GT68U nao nd
G001 nao nao
G001 nao nao
G001 nao nao
G001 nao nao
I tried to use the function mutate
df %>%
group_by(id)%>%
mutate(output = case_when(car1 == "nao" & car1 == "nao" ~ "nao",
car1 == "sim" & car1 == "sim" ~ "sim",
car1 == "sim" & car1 == "nao" ~ "nd",
TRUE ~ 0))
but I get the bug
Error: must be a Character vector, not a double vector
Run rlang::last_error()
to see Where the error occurred.