Examples for 'base::paste'


Concatenate Strings

Aliases: paste paste0

Keywords: character

### ** Examples

## When passing a single vector, paste0 and paste work like as.character.
paste0(1:12)
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12"
paste(1:12)        # same
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12"
as.character(1:12) # same
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12"
## If you pass several vectors to paste0, they are concatenated in a
## vectorized way.
(nth <- paste0(1:12, c("st", "nd", "rd", rep("th", 9))))
 [1] "1st"  "2nd"  "3rd"  "4th"  "5th"  "6th"  "7th"  "8th"  "9th"  "10th"
[11] "11th" "12th"
## paste works the same, but separates each input with a space.
## Notice that the recycling rules make every input as long as the longest input.
paste(month.abb, "is the", nth, "month of the year.")
 [1] "Jan is the 1st month of the year."  "Feb is the 2nd month of the year." 
 [3] "Mar is the 3rd month of the year."  "Apr is the 4th month of the year." 
 [5] "May is the 5th month of the year."  "Jun is the 6th month of the year." 
 [7] "Jul is the 7th month of the year."  "Aug is the 8th month of the year." 
 [9] "Sep is the 9th month of the year."  "Oct is the 10th month of the year."
[11] "Nov is the 11th month of the year." "Dec is the 12th month of the year."
paste(month.abb, letters)
 [1] "Jan a" "Feb b" "Mar c" "Apr d" "May e" "Jun f" "Jul g" "Aug h" "Sep i"
[10] "Oct j" "Nov k" "Dec l" "Jan m" "Feb n" "Mar o" "Apr p" "May q" "Jun r"
[19] "Jul s" "Aug t" "Sep u" "Oct v" "Nov w" "Dec x" "Jan y" "Feb z"
## You can change the separator by passing a sep argument
## which can be multiple characters.
paste(month.abb, "is the", nth, "month of the year.", sep = "_*_")
 [1] "Jan_*_is the_*_1st_*_month of the year." 
 [2] "Feb_*_is the_*_2nd_*_month of the year." 
 [3] "Mar_*_is the_*_3rd_*_month of the year." 
 [4] "Apr_*_is the_*_4th_*_month of the year." 
 [5] "May_*_is the_*_5th_*_month of the year." 
 [6] "Jun_*_is the_*_6th_*_month of the year." 
 [7] "Jul_*_is the_*_7th_*_month of the year." 
 [8] "Aug_*_is the_*_8th_*_month of the year." 
 [9] "Sep_*_is the_*_9th_*_month of the year." 
[10] "Oct_*_is the_*_10th_*_month of the year."
[11] "Nov_*_is the_*_11th_*_month of the year."
[12] "Dec_*_is the_*_12th_*_month of the year."
## To collapse the output into a single string, pass a collapse argument.
paste0(nth, collapse = ", ")
[1] "1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th"
## For inputs of length 1, use the sep argument rather than collapse
paste("1st", "2nd", "3rd", collapse = ", ") # probably not what you wanted
[1] "1st 2nd 3rd"
paste("1st", "2nd", "3rd", sep = ", ")
[1] "1st, 2nd, 3rd"
## You can combine the sep and collapse arguments together.
paste(month.abb, nth, sep = ": ", collapse = "; ")
[1] "Jan: 1st; Feb: 2nd; Mar: 3rd; Apr: 4th; May: 5th; Jun: 6th; Jul: 7th; Aug: 8th; Sep: 9th; Oct: 10th; Nov: 11th; Dec: 12th"
## Using paste() in combination with strwrap() can be useful
## for dealing with long strings.
(title <- paste(strwrap(
    "Stopping distance of cars (ft) vs. speed (mph) from Ezekiel (1930)",
    width = 30), collapse = "\n"))
[1] "Stopping distance of cars\n(ft) vs. speed (mph) from\nEzekiel (1930)"
plot(dist ~ speed, cars, main = title)
plot of chunk example-base-paste-1
## 'recycle0 = TRUE' allows more vectorized behaviour, i.e. zero-length recycling :
valid <- FALSE
val <- pi
paste("The value is", val[valid], "-- not so good!")
[1] "The value is  -- not so good!"
paste("The value is", val[valid], "-- good: empty!", recycle0=TRUE) # -> character(0)
character(0)
## When  'collapse = <string>',  the result is a length-1 string :
paste("foo", {}, "bar", collapse="|")                  # |-->  "foo  bar"
[1] "foo  bar"
paste("foo", {}, "bar", collapse="|", recycle0 = TRUE) # |-->  ""
[1] ""
## all empty args
paste(    collapse="|")                  # |-->  ""  as do all these:
[1] ""
paste(    collapse="|", recycle0 = TRUE)
[1] ""
paste({}, collapse="|")
[1] ""
paste({}, collapse="|", recycle0 = TRUE)
[1] ""

[Package base version 4.2.3 Index]