regex - Pasting character vectors, removing NA's and separators between NAs -
i've got several character vectors want paste together. problem of character vectors pretty sparse. so, when paste them, na's , separators. how can efficiently remove na's , separators while still joining vectors?
i've got 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 want:
[1,] "goats, forever" [2,] "goats" [3,] "spatula, ...yes" [4,] <na> [5,] "rectitude" [6,] "boink"
there inefficient , tedious ways of doing lot of regular expressions , string splitting. quick/simple?
not lot of regex, 1 line , 1 more 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
Comments
Post a Comment