jeudi 27 février 2020

IFELSE in R returning incorrect values

I have a data frame with categorical values that were entered manually and there are several mistakes. Someone cleaned up the bad data and I loaded that into R and merged that with the rest of my data. Everything so far is good.

As an example, let's say this is the data I have with the original (mix of good and bad data) in the "Value" column and the corrections of the bad data in the "Value_Clean" column. Obviously this is a small example but my actual data frame has dozens of corrections of different values and several thousand rows.

test <- data.frame(ID = c(1, 2, 3)
               , Value = c("Discuss plan", "Discuss plan", "Discuss paln")
               , Value_Clean = c(NA, NA, "Discuss plan"))

I would like to create a new column called "Value_Final" that has "Discuss plan" for IDs 1, 2, and 3.

It seems pretty straightforward that I should be able to accomplish this with an ifelse:

test$Value_Final <- ifelse(is.na(test$Value_Clean), test$Value, test$Value_Clean)

However, when I do that I get the following:

> test
  ID        Value  Value_Clean Value_Final
1  1 Discuss plan         <NA>           2
2  2 Discuss plan         <NA>           2
3  3 Discuss paln Discuss plan           1

What the hell? I feel like I've done similar things with ifelse in R without running into this issue, so what is going?

Thanks!

Aucun commentaire:

Enregistrer un commentaire