paste and paste0
paste glues pieces of text together, like linking train cars into one train. paste0 does it with no space in between, and you can choose your own separator.
In this page:
Syntax
paste(string1, string2, sep = " ")
paste0(string1, string2)
paste(vector_name, collapse = ", ")
paste and paste0
paste() concatenates strings together, inserting a separator (a space by default) between each argument. paste0() is shorthand for paste(..., sep = ""), concatenating with no separator at all.
Both also work element-wise on vectors, pairing up corresponding elements.
Note:
paste0(a, b) is a common shortcut for building a string with no space, like a file path or identifier.
Example: paste and paste0
first <- "Hello"
second <- "World"
cat(paste(first, second), "\n")
cat(paste0(first, second), "\n")
cat(paste(first, second, sep = "-"), "\n")
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Expecting
paste("a", "b")to have no space, when the default separator is a space; usepaste0()orsep = "". - Passing a vector and expecting one string, when
paste()gives a vector and you needcollapse = ", ". - Using
+to join strings, which is an error in R.
🔒
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: