I'm trying to recode some factor levels using dplyr::mutate(), so that all "MOT" and "CHI" factor levels remain as they are, and all others turn to "OTH".
My data looks like this:
subj | speaker | word
1 MOT apple
1 CHI baby
1 SI1 baby
2 CHI dog
2 CHI cat
2 FAT cat
And I want it to looks like this:
subj | speaker | word
1 MOT apple
1 CHI baby
1 OTH baby
2 CHI dog
2 CHI cat
2 OTH cat
My code looks like this:
new.df <- data %>%
dplyr::select(subj, speaker) %>%
mutate(speaker = factor(speaker),
speaker = ifelse(speaker %in% c("CHI", "MOT"), speaker, "OTH"))
However, when I run this script some of the levels are converted to "OTH" but all of the others change to numbers. This happens regardless of whether I include the factor(speaker) command:
subj | speaker | word
1 175 apple
1 86 baby
1 OTH baby
2 86 dog
2 86 cat
2 OTH cat
It is changing the factors to "OTH" ok but the TRUE element of my ifelse code appears to be the problem.
Many thanks!
Why I am not sure exactly but if you read the help menu for factor: ?factor it mentions first converting to as.character.
new.df <- data %>%
dplyr::select(subj, speaker) %>%
mutate(speaker = factor(speaker),
speaker = ifelse(speaker %in% c("CHI", "MOT"), as.character(speaker), "OTH"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With