Consider the following:
a = 1:10
paste("The list is:", a)
And the result would be:
[1] "The list is: 1" "The list is: 2" "The list is: 3" "The list is: 4"
[5] "The list is: 5" "The list is: 6" "The list is: 7" "The list is: 8"
[9] "The list is: 9" "The list is: 10"
I have solved it by:
paste("The list is:", paste(a, collapse=", "))
# "The list is: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10"
Is there any better idea?
I guess it depends what you want it for. If you are pasting this together to display in the R console, say as a note or information, then cat()
works a bit more intuitively:
R> cat("The list is:", a, "\n")
The list is: 1 2 3 4 5 6 7 8 9 10
or
R> cat("The list is:", a, fill = TRUE)
The list is: 1 2 3 4 5 6 7 8 9 10
If you want the actual character string as an R object I don't think you'll get much simpler than the paste()
idiom you show.
You can use:
paste(c("The list is:", a), collapse= " ")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With