The “paste” Function in R

  • Package: Base R (No specific package, it’s a built-in function)

  • Purpose: To concatenate (paste) vectors after converting them to character.

  • General Class: String Manipulation

  • Required Argument(s):

    • ...: A sequence of character vectors to be concatenated.

  • Notable Optional Arguments:

    • sep: A character string used to separate the concatenated elements. The default is a single space.

    • collapse: A character string used to concatenate the result into a single string.

  • Example:

  • # Starting name of the File

    File_Name = "Text_File"

    # Character between name and number

    File_Sep = "_"

    # File extension

    File_Ext = ".txt"

    # The number of file names

    File_N = 10

    # The names and numbers pasted together into a vector

    File_Vec = paste(File_Name, File_Sep, 1:File_N, File_Ext, sep = "")

    # Print the file names

    print(File_Vec)

    # Example using collapse

    paste(c("These", "were", "seperate", "vector", "elements!"), collapse = " ")

  • In this example, the paste function and the “sep” argument generate a vector of file names. This can be used when you save multiple files from a process being run, such as during a simulation study. The last line of code demonstrates the behavior of the “collapse” argument. The “collapse” argument joins the elements of a vector into a single character element (or a vector of length 1 if you prefer to think of it like that).

Previous
Previous

The “paste0” Function in R

Next
Next

The “t” Function in R