Reputation: 922
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?
Upvotes: 1
Views: 35
Reputation: 886948
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
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")
Upvotes: 2