R paste() Function - String Concatenation
The R paste() function is used to concatenate multiple strings or vectors together.
paste0() is the separator-free version of paste(), equivalent to paste(sep = "").
The syntax of the paste() function is as follows:
paste(..., sep = " ", collapse = NULL)
paste0(..., collapse = NULL)
Parameter Description:
-
... The strings or vectors to concatenate.
-
sep The separator used when concatenating, a single space by default.
-
collapse Further combine the concatenated vector into one string, using collapse as the separator.
Example
Basic concatenation (separated by a space by default)
result1
<-
paste
(
"Hello"
,
"RUNOOB"
,
"R"
)
(
result1
)
Specify a separator
result2
<-
paste
(
"2026"
,
"05"
,
"11"
, sep
=
"-"
)
(
result2
)
paste0 with no separator
result3
<-
paste0
(
"ID_"
,
1
:
5
)
(
result3
)
Use collapse to combine a vector into one string
words
<-
c
(
"R"
,
"is"
,
"powerful"
)
sentence
<-
paste
(
words, collapse
=
" "
)
(
sentence
)
The output of the code above is:
[1] "Hello RUNOOB R"
[1] "2026-05-11"
[1] "ID_1" "ID_2" "ID_3" "ID_4" "ID_5"
[1] "R is powerful"
paste() is often used to dynamically generate labels or file names:
Example
Generate month labels
months
<-
paste
(
"Month"
,
1
:
12
, sep
=
""
)
(
months
[
1
:
5
]
)
Generate file paths
path
<-
paste0
(
"data/"
,
c
(
"sales"
,
"inventory"
,
"customers"
)
,
".csv"
)
(
path
)
The output of the code above is:
[1] "月份1" "月份2" "月份3" "月份4" "月份5"
[1] "data/sales.csv" "data/inventory.csv"
[3] "data/customers.csv"
R Language Examples