Jazzmatazz
Jazzmatazz

Reputation: 645

How do I add characters to the beginning or end of result of paste0?

I am trying to create a character string that looks something like "c,1,2,3,4,5,6,7,8"

I am able to get the number part of the string by doing:

paste0(1:200, collapse = ",")

How would I add "c," to the beginning of the result of paste0? Alternatively, how could I join ",c" end of the result?

Upvotes: 2

Views: 1747

Answers (3)

akrun
akrun

Reputation: 887213

We can wrap with paste

paste0("c(", paste0(1:200, collapse = ","), ")")

Or with sprintf

sprintf("c(%s)", paste0(1:200, collapse=","))

If we need only 'c', then use

sprintf("c,%s", paste0(1:200, collapse = ","))

Upvotes: 6

G. Grothendieck
G. Grothendieck

Reputation: 269664

Create a vector made up of c and 1:8 and then use toString:

toString(c("c", 1:8))
## [1] "c, 1, 2, 3, 4, 5, 6, 7, 8"

Upvotes: 2

James B
James B

Reputation: 474

You just want the string c, not the function right?

paste0(c("c", 1:8), collapse = ",")

Upvotes: 4

Related Questions