How to omit NA values while pasting numerous column values together?

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)))

Leave a Comment