0

I have a dataset and i trying to use mutate to replace or rename the data inside a column given different conditions.

I have generate a summary using code:

summary(s_r_data_no_na_split_rename_conv)

and show beclow info.

enter image description here

I am trying to make the changes as follow:

  • 1 becomes "Some High School"
  • 2 becomes "High School"
  • 3 becomes "Vocational School"
  • 4 becomes "College"

But i only can change 1 of the category and if i repeat using below code, the previous converted value become NA.

s_r_data_no_na_split_rename_conv_edu <- s_r_data_no_na_split_rename_conv %>% 
  mutate(Education = ifelse(as.character(Education) == "1", "Some High School", as.factor(Education)))
  • You could use `dplyr::recode` ([docu](https://dplyr.tidyverse.org/reference/recode.html), which exists exactly for that purpose) – dario Jul 04 '20 at 12:02
  • I think i manage to find an answer s_r_data_no_na_split_rename_conv_edu <- s_r_data_no_na_split_rename_conv %>% mutate(Education = ifelse(Education == "1", "Some High School", Education)) %>% mutate(Education = ifelse(Education == "2", "High School", Education)) %>% mutate(Education = ifelse(Education == "3", "Vocational School", Education)) %>% mutate(Education = ifelse(Education == "4", "College", Education)) – Newbie coder Jul 04 '20 at 12:34

0 Answers0