Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- foo <- LETTERS[1:4]
- foo[4] <- NA
- foo
- [1] "A" "B" "C" NA
- paste(1:4, foo, sep = ", ")
- [1] "1, A" "2, B" "3, C" "4, NA"
- [1] "1, A" "2, B" "3, C" "4"
- sub(', NA$', '', paste(1:4, foo, sep = ", "))
- [1] "1, A" "2, B" "3, C" "4"
- paste2 <- function(...,sep=", ") {
- L <- list(...)
- L <- lapply(L,function(x) {x[is.na(x)] <- ""; x})
- gsub(paste0("(^",sep,"|",sep,"$)"),"",
- gsub(paste0(sep,sep),sep,
- do.call(paste,c(L,list(sep=sep)))))
- }
- foo <- c(LETTERS[1:3],NA)
- bar <- c(NA,2:4)
- baz <- c("a",NA,"c","d")
- paste2(foo,bar,baz)
- # [1] "A, a" "B, 2" "C, 3, c" "4, d"
- gsub(", $", "", paste(1:4, ifelse(is.na(foo), "", foo), sep = ", "))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement