0

I've got several character vectors that I want to paste together. The problem is that some of the character vectors are pretty sparse. So, when I paste them, I get NA's and extra separators. How can I efficiently remove the NA's and extra separators while still joining the vectors?

I've got something like:

n1 = c("goats", "goats", "spatula", NA, "rectitude", "boink")
n2 = c("forever", NA, "...yes", NA, NA, NA)
cbind(paste(n1,n2, sep=", "))

which gives me:

[1,] "goats, forever" 
[2,] "goats, NA"      
[3,] "spatula, ...yes"
[4,] "NA, NA"         
[5,] "rectitude, NA"  
[6,] "boink, NA" 

but I want:

[1,] "goats, forever" 
[2,] "goats"          
[3,] "spatula, ...yes"
[4,] <NA>
[5,] "rectitude"      
[6,] "boink"

There are clearly inefficient and tedious ways of doing this with a lot of regular expressions and string splitting. But anything quick/simple?

generic_user
  • 3,430
  • 3
  • 32
  • 56

3 Answers3

5

Not a lot of regex, just 1 line and 1 more to replace 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
RJ-
  • 2,919
  • 3
  • 28
  • 35
5

Code (no regex or string splitting):

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

Result:

> 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"
Thomas
  • 43,637
  • 12
  • 109
  • 140
1

Here's an option using the qdap package (though the other options seem better to me as they use base R):

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

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

Or...

## gsub(" ", ", ", blank2NA(gsub("NA| NA", "", paste(n1, n2))))
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519