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 vector
s, 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")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…