Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

Concatenating two columns together in R with NAs

I want to create a column (outcome) that would be a concatenation of two other columns.

col1 col2     Outcome
A     NA       A
A     B        AB
NA    NA       NA
NA    B        B
A     B        AB

I tried:

df$Outcome = c(df$col1, df$col2)

But it does not work. What is the simplest way to do this?

question from:https://stackoverflow.com/questions/65929793/concatenating-two-columns-together-in-r-with-nas

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

We can unite

library(dplyr)
library(tidyr)
df %>%
    unite(Outcome, col1, col2, sep="", na.rm = TRUE, remove = FALSE) %>%
    select(names(df), Outcome) %>%
    mutate(Outcome = na_if(Outcome, ""))

-output

#   col1 col2 Outcome
#1    A <NA>       A
#2    A    B      AB
#3 <NA> <NA>    <NA>
#4 <NA>    B       B
#5    A    B      AB

Or in base R in a single line

apply(df, 1, function(x) paste(na.omit(x), collapse=""))

Or it can be done in a hacky way by removing the NA (but it may fail)

na_if(gsub("NA", "", do.call(paste0, df)), "")

c is for concatenating. If we do the c on two vectors, the second vector is concatenated at the end of the first and it results in length equal to the sum of lengths of both vectors

data

df <- structure(list(col1 = c("A", "A", NA, NA, "A"), col2 = c(NA, 
 "B", NA, "B", "B")), row.names = c(NA, -5L), class = "data.frame")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...