| paste | R Documentation |
Concatenate vectors after converting to character.
paste (..., sep = " ", collapse = NULL, recycle0 = FALSE)paste0(..., collapse = NULL, recycle0 = FALSE)
... | one or moreR objects, to be converted to character vectors. |
sep | a character string to separate the terms. Not |
collapse | an optional character string to separate the results. Not |
recycle0 |
|
paste converts its arguments (viaas.character) to character strings, and concatenatesthem (separating them by the string given bysep). If thearguments are vectors, they are concatenated term-by-term to give acharacter vector result. Vector arguments are recycled as needed,with zero-length arguments being recycled to"" only ifrecycle0 is not trueorcollapse is notNULL.
Note thatpaste() coercesNA_character_, thecharacter missing value, to"NA" which may seemundesirable, e.g., when pasting two character vectors, or verydesirable, e.g. inpaste("the value of p is ", p).
paste0(..., collapse) is equivalent topaste(..., sep = "", collapse), slightly more efficiently.
If a value is specified forcollapse, the values in the resultare then concatenated into a single string, with the elements beingseparated by the value ofcollapse.
A character vector of the concatenated values. This will be of lengthzero if all the objects are, unlesscollapse is non-NULL, in whichcase it is"" (a single empty string).
If any input into an element of the result is in UTF-8 (and none aredeclared with encoding"bytes", seeEncoding),that element will be in UTF-8, otherwise in the current encoding inwhich case the encoding of the element is declared if the currentlocale is either Latin-1 or UTF-8, at least one of the correspondinginputs (including separators) had a declared encoding and all inputswere either ASCII or declared.
If an input into an element is declared with encoding"bytes",no translation will be done of any of the elements and the resultingelement will have encoding"bytes". Ifcollapse isnon-NULL, this applies also to the second, collapsing, phase, but sometranslation may have been done in pasting object together in the firstphase.
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)The New S Language.Wadsworth & Brooks/Cole.
toString typically callspaste(*, collapse=", ").String manipulation withas.character,substr,nchar,strsplit; further,cat which concatenates andwrites to a file, andsprintf for C like stringconstruction.
‘plotmath’ for the use ofpaste in plot annotation.
## When passing a single vector, paste0 and paste work like as.character.paste0(1:12)paste(1:12) # sameas.character(1:12) # same## 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))))## 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.")paste(month.abb, letters)## 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 = "_*_")## To collapse the output into a single string, pass a collapse argument.paste0(nth, collapse = ", ")## For inputs of length 1, use the sep argument rather than collapsepaste("1st", "2nd", "3rd", collapse = ", ") # probably not what you wantedpaste("1st", "2nd", "3rd", sep = ", ")## You can combine the sep and collapse arguments together.paste(month.abb, nth, sep = ": ", collapse = "; ")## 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"))plot(dist ~ speed, cars, main = title)## 'recycle0 = TRUE' allows more vectorized behaviour, i.e. zero-length recycling :valid <- FALSEval <- pipaste("The value is", val[valid], "-- not so good!")paste("The value is", val[valid], "-- good: empty!", recycle0=TRUE) # -> character(0)## When 'collapse = <string>', the result is a length-1 string :paste("foo", {}, "bar", collapse="|") # |--> "foo bar"paste("foo", {}, "bar", collapse="|", recycle0 = TRUE) # |--> ""## all empty argspaste( collapse="|") # |--> "" as do all these:paste( collapse="|", recycle0 = TRUE)paste({}, collapse="|")paste({}, collapse="|", recycle0 = TRUE)Add the following code to your website.
For more information on customizing the embed code, readEmbedding Snippets.