Home > Software engineering >  R coalesce two columns but keep both values if not NA
R coalesce two columns but keep both values if not NA

Time:12-28

I have a dataframe with two columns of related data. I want to create a third column that combines them, but there are lots of NAs in one or both columns. If both columns have a non-NA value, I want the new third column to paste both values. If either of the first two columns has an NA, I want the third column to contain just the non-NA value. An example with a toy data frame is below:

x <- c("a", NA, "c", "d")
y <- c("l", "m", NA, "o")
df <- data.frame(x, y)

# this is the new column I want to produce from columns x and y above
df$z <- c("al", "m", "c", "do")

I thought coalesce would solve my problem, but I can't find a way to keep both values if there is a value in both columns. Thanks in advance for any assistance.

CodePudding user response:

One posible solution:

df$z <- gsub("NA", "",paste0(df$x, df$y))

CodePudding user response:

Another possible solution:

library(dplyr)
df %>% 
  mutate(z = ifelse(is.na(x) | is.na(y), coalesce(x,y), paste0(x,y)))

CodePudding user response:

An option with unite

library(tidyr)
library(dplyr)
df %>% 
  unite(z, everything(), na.rm = TRUE, sep = "", remove = FALSE)
   z    x    y
1 al    a    l
2  m <NA>    m
3  c    c <NA>
4 do    d    o
  • Related