0

in R, Let's say I have strings like:

str0 <- NA
str1 <- c("aaa")
str2 <- NA
str3 <- c("bbb")
str4 <- NA

when I do:

paste(str0, str1, str2, str3, str4, sep=',')

I got:

,aaa,,bbb,

But what I really need is:

aaa,bbb

So how can I ignore the comma at begin, at last and at the middle (more then 2 commas)? Thanks in advance!

Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Huan Lu
  • 15
  • 5
  • 2
    `vec <- c(str0, str1, str2, str3, str4)` and then `paste(na.omit(vec), collapse = ",")` – Ronak Shah Aug 10 '20 at 10:14
  • @RonakShah Thank you, Ronak! I was stuck at this for a long time, I did not succeed with your method because str0-str4 in my program is another concatenated string, but I was using collapse, hence, in the end, it is not technically a "na.omitable" string, then I switched to sep=',' at last by which time I firstly use collapse=',', problem solved! thank you for inspiration! – Huan Lu Aug 11 '20 at 02:33

0 Answers0