You could use case_when from the dplyr
library:
df1$y <- case_when(
x == 1 ~ "s",
x == 2 ~ "t",
x == 3 ~ "u",
x == 4 ~ "v",
TRUE ~ "w"
)
Note that the final case above (TRUE
) is the blanket else condition which will catch all cases not matching any earlier conditions.