Search code examples
rrstudio-server

How to include a vector with multiple elements in a paste() statement?


Have a character vector called error.list with multiple elements in it. I tried to include the complete vector in my paste statement as shown below:

paste("Hi, Your data has the following errors:", error.list," Regards, XYZ", 
sep=''). 

But I am getting an error. The console says it can't add all the elements in a single paste statement. Can help me with this? is there another way to do it?


Solution

  • Concatenate Strings

    Your approach will work For inputs of length 1 but you have a vector.

    Because error.list is a vector, use one of the following methods:

    error.list <- c("a", "b", "c")
    
    paste(c("Hi, Your data has the following errors:", error.list, " Regards, XYZ"), sep = " ")
    

    Provide the output:

    [1] "Hi, Your data has the following errors:" "a"                                      
    [3] "b"                                       "c"                                      
    [5] " Regards, XYZ"
    

    One line use parameter collapse = :

    paste(c("Hi, Your data has the following errors:", error.list, " Regards, XYZ"), sep = "  ", collapse = " ")
    

    Provide the output:

    "Hi, Your data has the following errors: a b c  Regards, XYZ"
    

    or you can use paste0() with parameter collapse = to get one line output, for also using the error.list as vector:

    paste0(c("Hi, Your data has the following errors:", error.list, " Regards, XYZ"), collapse = " ")    
    

    Provide the output:

    [1] "Hi, Your data has the following errors: a b c Regards, XYZ"