Search code examples
rstring-concatenation

How can I print all the elements of a vector in a single string in R?


Sometimes I want to print all of the elements in a vector in a single string, but it still prints the elements separately:

notes <- c("do","re","mi")
print(paste("The first three notes are: ", notes,sep="\t"))

Which gives:

[1] "The first three notes are: \tdo" "The first three notes are: \tre"
[3] "The first three notes are: \tmi"

What I really want is:

The first three notes are:      do      re      mi

Solution

  • The simplest way might be to combine your message and data using one c function:

    paste(c("The first three notes are: ", notes), collapse=" ")
    ### [1] "The first three notes are:  do re mi"