我有几个要粘贴在一起的字符向量。问题是一些字符向量非常稀疏。所以,当我粘贴它们时,我会得到 NA 和额外的分隔符。如何在仍然加入向量的同时有效地删除 NA 和额外的分隔符?
我有类似的东西:
n1 = c("goats", "goats", "spatula", NA, "rectitude", "boink")
n2 = c("forever", NA, "...yes", NA, NA, NA)
cbind(paste(n1,n2, sep=", "))
这给了我:
[1,] "goats, forever"
[2,] "goats, NA"
[3,] "spatula, ...yes"
[4,] "NA, NA"
[5,] "rectitude, NA"
[6,] "boink, NA"
但我想要:
[1,] "goats, forever"
[2,] "goats"
[3,] "spatula, ...yes"
[4,] <NA>
[5,] "rectitude"
[6,] "boink"
使用大量正则表达式和字符串拆分显然有效率低下且乏味的方法。但是有什么快速/简单的吗?