EVOLUTION-MANAGER
Edit File: paste.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Concatenate Strings</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for paste {base}"><tr><td>paste {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Concatenate Strings</h2> <h3>Description</h3> <p>Concatenate vectors after converting to character. </p> <h3>Usage</h3> <pre> paste (..., sep = " ", collapse = NULL) paste0(..., collapse = NULL) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>...</code></td> <td> <p>one or more <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> objects, to be converted to character vectors.</p> </td></tr> <tr valign="top"><td><code>sep</code></td> <td> <p>a character string to separate the terms. Not <code><a href="NA.html">NA_character_</a></code>.</p> </td></tr> <tr valign="top"><td><code>collapse</code></td> <td> <p>an optional character string to separate the results. Not <code><a href="NA.html">NA_character_</a></code>.</p> </td></tr> </table> <h3>Details</h3> <p><code>paste</code> converts its arguments (<em>via</em> <code><a href="character.html">as.character</a></code>) to character strings, and concatenates them (separating them by the string given by <code>sep</code>). If the arguments are vectors, they are concatenated term-by-term to give a character vector result. Vector arguments are recycled as needed, with zero-length arguments being recycled to <code>""</code>. </p> <p>Note that <code>paste()</code> coerces <code><a href="NA.html">NA_character_</a></code>, the character missing value, to <code>"NA"</code> which may seem undesirable, e.g., when pasting two character vectors, or very desirable, e.g. in <code>paste("the value of p is ", p)</code>. </p> <p><code>paste0(..., collapse)</code> is equivalent to <code>paste(..., sep = "", collapse)</code>, slightly more efficiently. </p> <p>If a value is specified for <code>collapse</code>, the values in the result are then concatenated into a single string, with the elements being separated by the value of <code>collapse</code>. </p> <h3>Value</h3> <p>A character vector of the concatenated values. This will be of length zero if all the objects are, unless <code>collapse</code> is non-NULL in which case it is a single empty string. </p> <p>If any input into an element of the result is in UTF-8 (and none are declared with encoding <code>"bytes"</code>, see <code><a href="Encoding.html">Encoding</a></code>), that element will be in UTF-8, otherwise in the current encoding in which case the encoding of the element is declared if the current locale is either Latin-1 or UTF-8, at least one of the corresponding inputs (including separators) had a declared encoding and all inputs were either ASCII or declared. </p> <p>If an input into an element is declared with encoding <code>"bytes"</code>, no translation will be done of any of the elements and the resulting element will have encoding <code>"bytes"</code>. If <code>collapse</code> is non-NULL, this applies also to the second, collapsing, phase, but some translation may have been done in pasting object together in the first phase. </p> <h3>References</h3> <p>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) <em>The New S Language</em>. Wadsworth & Brooks/Cole. </p> <h3>See Also</h3> <p><code><a href="toString.html">toString</a></code> typically calls <code>paste(*, collapse=", ")</code>. String manipulation with <code><a href="character.html">as.character</a></code>, <code><a href="substr.html">substr</a></code>, <code><a href="nchar.html">nchar</a></code>, <code><a href="strsplit.html">strsplit</a></code>; further, <code><a href="cat.html">cat</a></code> which concatenates and writes to a file, and <code><a href="sprintf.html">sprintf</a></code> for C like string construction. </p> <p>‘<a href="../../grDevices/html/plotmath.html">plotmath</a>’ for the use of <code>paste</code> in plot annotation. </p> <h3>Examples</h3> <pre> ## When passing a single vector, paste0 and paste work like as.character. paste0(1:12) paste(1:12) # same as.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 collapse paste("1st", "2nd", "3rd", collapse = ", ") # probably not what you wanted paste("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) </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>