andy
andy

Reputation: 194

R - create a string of quoted numbers

I have a vector of numbers a <- 1:5 and I want to end up with a string like:

b <- " '1', '2', '3', '4', '5' "

(a string of strings)

I cant figure out how to do this succinctly in R.

I need to pass this as part of an SQL statement; the DB (Oracle) stores these "numbers" as characters hence this conversion.

Upvotes: 0

Views: 45

Answers (2)

Merijn van Tilborg
Merijn van Tilborg

Reputation: 5887

paste("'", 1:5, "'", collapse = ", ", sep = "") [1] "'1', '2', '3', '4', '5'"

Upvotes: 0

Paul
Paul

Reputation: 2959

paste0() with collapse = ", " is what you want:

a <- 1:5
b <- paste0("'", a, "'", collapse = ", ")
b

# [1] "'1', '2', '3', '4', '5'"

Upvotes: 1

Related Questions