R Paste


paste() function concatenates vectors after converting to character. paste0() concatenates all elements without separator.


paste(..., sep = " ", collapse = NULL)
paste0(..., collapse = NULL)


...: one or more R objects, to be converted to character vectors.
sep: a character string to separate the terms.
collapse: an optional character string to separate the results.

> paste("good", "job")
[1] "good job"
> paste("good", "job", sep=" - ")
[1] "good - job"
> paste(letters[1:4])
[1] "a" "b" "c" "d"
> paste(letters[1:4],collapse=",")
[1] "a,b,c,d"


The arguments can be vectors. If one is shorter than the other, the shorter one will be used repeatly.

> paste("G",1:4,sep="")
[1] "G1" "G2" "G3" "G4"
> paste0("G",1:4)  #same result
[1] "G1" "G2" "G3" "G4"

Join multiple vectors:

> paste("G",1:4,letters[1:4],sep="_")
[1] "G_1_a" "G_2_b" "G_3_c" "G_4_d"
> paste("G",1:4,letters[1:4],sep="_", collapse=" > ")
[1] "G_1_a > G_2_b > G_3_c > G_4_d"

endmemo.com © 2024  | Terms of Use | Privacy | Home