Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

R paste() Function - String Concatenation

R paste() Function - String Concatenation

R Language Examples R Language Examples

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:

Example

Basic concatenation (separated by a space by default)

result1

<-

paste

(

"Hello"

,

"RUNOOB"

,

"R"

)

print

(

result1

)

Specify a separator

result2

<-

paste

(

"2026"

,

"05"

,

"11"

, sep

=

"-"

)

print

(

result2

)

paste0 with no separator

result3

<-

paste0

(

"ID_"

,

1

:

5

)

print

(

result3

)

Use collapse to combine a vector into one string

words

<-

c

(

"R"

,

"is"

,

"powerful"

)

sentence

<-

paste

(

words, collapse

=

" "

)

print

(

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

=

""

)

print

(

months

[

1

:

5

]

)

Generate file paths

path

<-

paste0

(

"data/"

,

c

(

"sales"

,

"inventory"

,

"customers"

)

,

".csv"

)

print

(

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 R Language Examples