1

I would like to turn vector

a <- 1:5

into a string

"1_2_3_4_5"

How to do this?

paste(a,sep="_")

does not work this way.

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385

3 Answers3

2

You could try collapse

paste(a, collapse="_")
#[1] "1_2_3_4_5"
akrun
  • 874,273
  • 37
  • 540
  • 662
2

you can try this

paste0(a, collapse="_")
# [1] "1_2_3_4_5"
Mamoun Benghezal
  • 5,264
  • 7
  • 28
  • 33
2

Just for fun:

 gsub(', ', '_', toString(a))
 #[1] "1_2_3_4_5"
Colonel Beauvel
  • 30,423
  • 11
  • 47
  • 87