Home > Software design >  how to replace NA with the values from another column
how to replace NA with the values from another column

Time:05-28

I would like to replace NA in the following data frame with values from the variable a.

df.NA <- data.frame(
  a = c("value1", "value2", "value3", "value4", "value5"),
  b = c(NA, 6, 7, NA,8),
  c = c(9, NA, NA, 10, 11),
  d = c(NA,NA,NA, 12, 13)
)

The final data frame should look like this

df.noNA <- data.frame(
  a = c("value1", "value2", "value3", "value4", "value5"),
  b = c("value1", 6, 7, "value4",8),
  c = c(9, "value2", "value3", 10, 11),
  d = c("value1","value2","value3", 12, 13)
)

Is there an easy way to achieve this?

Thanks!

CodePudding user response:

You can use mutate to apply an ifelse across every column.

library(dplyr)

df.NA %>%
   mutate(across(everything(), ~ ifelse(is.na(.x), a, .x)))
#>        a      b      c      d
#> 1 value1 value1      9 value1
#> 2 value2      6 value2 value2
#> 3 value3      7 value3 value3
#> 4 value4 value4     10     12
#> 5 value5      8     11     13

CodePudding user response:

Another possible solution:

library(dplyr)

df.NA %>% 
  mutate(across(-a, ~ coalesce(.x, a)))

#>   a b  c  d
#> 1 1 1  9  1
#> 2 2 6  2  2
#> 3 3 7  3  3
#> 4 4 4 10 12
#> 5 5 8 11 13

CodePudding user response:

in Base R:

ids <- is.na(df.NA)

replace(df.NA, ids, df.NA[which(ids, TRUE)[,1],'a'])
      a      b      c      d
1 value1 value1      9 value1
2 value2      6 value2 value2
3 value3      7 value3 value3
4 value4 value4     10     12
5 value5      8     11     13
  •  Tags:  
  • r na
  • Related