Reputation: 194
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
Reputation: 5887
paste("'", 1:5, "'", collapse = ", ", sep = "") [1] "'1', '2', '3', '4', '5'"
Upvotes: 0
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