I want to insert several columns with some text where they are combined, only if at least one is not NA, and the current solution works for me, but it is cumbersome. So I'm wondering if there is an easier way to do this (create a βcombineβ column below). I would like to use tidyr, but there seems to be no way to specify how to handle missing values ββin unite ()
Thank you and I hope that I have not missed anything obvious.
df = data.frame(num=c(1,2,NA,NA),place=c("Rome",NA,"Paris",NA))
df$combine[!is.na(df$num)|!is.na(df$place)] =
paste(df$num[!is.na(df$num)|!is.na(df$place)],
"days in",df$place[!is.na(df$num)|!is.na(df$place)])
source
share