I have a data frame dd2
with hundreds of columns and what I need to do is paste all these column values together omitting any NA
values. If I do so
You could try na.omit()
to omit the values, then paste. Also, you could use toString()
, as it is the equivalent of paste(..., collapse = ", ")
.
apply(dd2, 1, function(x) toString(na.omit(x)))
# [1] "A, AK2, PPT" "B, HFM1, PPT" "C, TRR"
# [4] "D, TRR, RTT, GGT" "E, RTT"
If you have specific columns you are using then
apply(dd2[, cols], 1, function(x) toString(na.omit(x)))