Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my factor levels turning to numbers with dplyr::mutate()?

Tags:

r

dplyr

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!

like image 375
Catherine Laing Avatar asked Jan 20 '26 02:01

Catherine Laing


1 Answers

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")) 
like image 68
Alex Dometrius Avatar answered Jan 22 '26 21:01

Alex Dometrius