user11889740
user11889740

Reputation:

Filling NA with the information of another column

tablenew <- table %>%
  mutate (i_a_i = case_when (
    INaam != 'NA' ~ INaam[],
    INaam == 'NA' ~ ANaam[],
    ANaam  == 'NA' & INaam == 'NA' ~ InNaam[]
    ))

what i basically want if INaam has missing data it will use the name in column Anaam and if both of these are missing NA then use InNaam. It doesn't really does completely what I want. Could you please help me out?

Upvotes: 1

Views: 47

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 388862

You can check NA values using is.na, also I think this can be reduced to only two conditions

library(dplyr)
table %>%
   mutate(i_a_i = case_when(
                     is.na(INaam) ~ Anaam,
                     TRUE ~ INaam))

If there is missing value in INaam you want to use Anaam value and for non-missing value use INaam.

Or just use ifelse

table$i_a_i <- with(table, ifelse(is.na(INaam), Anaam, INaam))

Upvotes: 1

Related Questions