Tenho esse 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"
))
Gostaria de criar uma nova coluna baseada nos valores de car1 para o conjunto de linhas de cada id. Caso o id sempre tenha car1 "sim" ou "nao" a nova coluna apenas replica a informação. Caso o id tenha "sim" ou "nao" a nova coluna deve apresentar "nd". De forma que ficasse assim:
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
Tentei usar a função 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))
mas recebo o erro
Error: must be a character vector, not a double vector
Run rlang::last_error()
to see where the error occurred.