0

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
Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
Ali Inayat
  • 41
  • 5
  • can someone please edit my question and make tables neat. don't understand what I'm doing wrong – Ali Inayat Sep 26 '22 at 07:45
  • We can't compare NA, try: `NA != NA` or `NA != 1`, so to check if it is NA, use `is.na(x)`. Also, useful function instead of if_else try [coalesce](https://dplyr.tidyverse.org/reference/coalesce.html) – zx8754 Sep 26 '22 at 08:46

2 Answers2

1
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.

Chris Ruehlemann
  • 20,321
  • 4
  • 12
  • 34
0

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

jk = ifelse(is.na(a6b), a6, a6b)
Julian
  • 6,586
  • 2
  • 9
  • 33
Arduan
  • 253
  • 1
  • 11
  • but can you also explain what is wrong with my code. Since I'm also trying to say the same thing that if a6b is not NA take value of a6b otherwise a6 – Ali Inayat Sep 26 '22 at 07:48