vendredi 19 janvier 2018

Ifelse generating numeric values in r

I know similar questions have been asked but I can't quite find the answer I'm looking for. I'm trying to match some characters across two columns in a dataframe:

 num   orig   new   match
 1      p      p      T
 2      t      g      F
 3      p      b      F
 4      d      d      T
 5      g      g      T

I want to create a new column called 'final', and if 'match' is true, I want to copy the value from orig into it. If 'match' is false, I want to copy the value from 'new' into it. Should be easy!

I use the code:

 data <- data %>% mutate(final = ifelse(match == T, orig, new))

When I run this, everything in column 'final' turns numeric.

I've also tried if_else, which generates the message "In [<-.factor(*tmp*, i, value = c(13L, 13L, 2L, 13L, 13L, 4L, : invalid factor level, NA generated" and turns most of my FALSE values to NAs.

Aucun commentaire:

Enregistrer un commentaire