Home > Back-end >  How to add new column conditional to NA in one column of data
How to add new column conditional to NA in one column of data

Time:09-26

I have been trying to make a new column in data set, lets say jk. I want in this column the values from column a6 if value in column a6b is NA, otherwise I need the value from column a6b. I have written the following line of code for this operation:

Combined_Data %>% mutate(jk=if_else(a6b!= NA , a6b, a6))

But instead if intended result, this code is converting all values in jk column to NAs.

What I have:

df
  a6 a6b
1 45  NA
2 62  32
3 NA  55
4 92 200

What I want:

df
  a6 a6b  jk
1 45  NA  45
2 62  32  32
3 NA  55  55
4 92 200 200

What I'm getting:

df
  a6 a6b  jk
1 45  NA  NA
2 62  32  NA
3 NA  55  NA
4 92 200  NA

CodePudding user response:

df %>%
  mutate(jk = if_else(!is.na(a6b), a6b, a6))

Here I mostly follow your code. However, I recommend reversing the logic from negative to positive, thus:

df %>%
  mutate(jk = if_else(is.na(a6b), a6, a6b))

NA, understood as missing values, must be referred to with the function is.na. The format a6b != "NA" (with quote marks!) works if the value is the string NA, which are not missing values but strings. Since none of your observations in the two original columns are such strings, the ifelse command obviously, and correctly, returns NA throughout.

CodePudding user response:

Use ifelse this way: condition, value if true, value if false:

jk = ifelse(is.na(a6b), a6, a6b)
  • Related