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

后端 未结 1 923
予麋鹿
予麋鹿 2020-11-28 16:35

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

相关标签:
1条回答
  • 2020-11-28 17:03

    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)))
    
    0 讨论(0)
提交回复
热议问题