SoFunction
Updated on 2025-03-01

Explanation of the difference between c function and paste function in R language

c() function:Connect elements in brackets, not creating vectors

paste() function:Connect elements in brackets

For example

c(1, 2:4), the result is 1 2 3 4

paste(1, 2:4), the result is "1 2" "1 3" "1 4"

c(2, "and"), result is "2" "and"

paste(2, "and"), the result is "2 and"

Supplement: Detailed explanation of the parameters sep and collapse of paste function in R language

There are two main functions used in R language for splicing strings:

paste (..., sep = " ", collapse = NULL)
paste0(..., collapse = NULL)

Among them, the reduced version of paste0 function paste function lacks one parameter sep. The... in the parameters represent the object you want to splice, and the parameters followed indicate the way of splicing.

Usually we have three requirements for string stitching:

A bunch of individual strings are spliced ​​together;

Two or more string objects are spliced ​​together according to the corresponding relationship of the element;

A string is concatenated together.

Give some examples:

paste('hello','world','!')  #A bunch of individual strings spliced ​​together## [1] "hello world !"
paste(c('A','B','C'),c(1,2,3)) #Two or more string objects are spliced ​​together according to the corresponding relationship of the element## [1] "A 1" "B 2" "C 3"
paste(1:10) #A string concatenated together## [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10"

So what is the usage of the two parameters?

The parameter used when dealing with the first two requirements is sep, that is, splicing between multiple strings;

When dealing with the third requirement, the parameter used is collapse, that is, what symbols or formats are used to connect a string when splicing;

Give some examples:

paste('hello','world','!')  #A bunch of individual strings are spliced ​​together, if you want to use '_'paste('hello','world','!',sep = '_')
## [1] "hello_world_!"
paste(c('A','B','C'),c(1,2,3)) #Two or more string objects are spliced ​​together according to the corresponding relationship of the element. If you want to use '-'paste(c('A','B','C'),c(1,2,3),sep = '-')
## [1] "A-1" "B-2" "C-3"
#In addition, it should be noted that if the lengths of two or more objects passed in are different, the corresponding relationship will change, for examplepaste(c('A','B','C','D','E'),c(1,2),sep = '-')
## [1] "A-1" "B-2" "C-1" "D-2" "E-1"
paste(1:10) #A string is connected together. The third requirement is very different from the above. The above two requirements are passed in multiple objects, while the third requirement only passes in one object at the first parameter position of the function. If you need to use the '~' connectionpaste(1:10, collapse = '~')
## [1] "1~2~3~4~5~6~7~8~9~10"

There is such an equation for the reduced version of paste0:

paste0(“a”, “b”) == paste(“a”, “b”, sep=”“)

I personally think it’s enough to be familiar with paste. After all, paste has more functions and is more flexible to use.

The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.