0

我有几个要粘贴在一起的字符向量。问题是一些字符向量非常稀疏。所以,当我粘贴它们时,我会得到 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"

使用大量正则表达式和字符串拆分显然有效率低下且乏味的方法。但是有什么快速/简单的吗?

4

3 回答 3

5

代码(无正则表达式或字符串拆分):

vec <- apply(cbind(n1,n2),1,function(x)
    ifelse(all(is.na(x)), NA, paste(na.omit(x),collapse=", ")) )

结果:

> vec # as a vector
[1] "goats, forever"  "goats"  "spatula, ...yes"  NA  "rectitude"  "boink"

> cbind(vec) # as a matrix
     vec              
[1,] "goats, forever" 
[2,] "goats"          
[3,] "spatula, ...yes"
[4,] NA               
[5,] "rectitude"      
[6,] "boink"
于 2013-08-13T09:31:53.137 回答
5

没有很多正则表达式,只需 1 行和 1 个替换NA

n1 <- c("goats", "goats", "spatula", NA, "rectitude", "boink")
n2 <- c("forever", NA, "...yes", NA, NA, NA)
n3 <- cbind(paste(n1,n2, sep=", "))
n3 <- gsub("(, )?NA", "", n3)
n3[n3==""] <- NA
于 2013-08-13T09:28:19.600 回答
1

这是使用 qdap 包的选项(尽管其他选项对我来说似乎更好,因为它们使用 base R):

library(qdap)
gsub(" ", ", ", blank2NA(Trim(gsub("NA", "", paste(n1, n2)))))

## [1] "goats, forever"  "goats"           "spatula, ...yes" NA               
## [5] "rectitude"       "boink"

或者...

## gsub(" ", ", ", blank2NA(gsub("NA| NA", "", paste(n1, n2))))
于 2013-08-13T13:15:01.860 回答