Paste function in R is used to concatenate Vectors by converting them into character. paste0 function in R simply concatenates the vector without any separator. lets see an example of paste() Function in R and Paste0() Function in R. Lets see an example on applying paste() and paste0() function for the dataframe.
- concatenate the vectors or strings by using paste() function.
- concatenate the columns of the dataframe using paste() function in R.
- concatenate the vectors or strings by using paste0() function.
- concatenate the columns of the dataframe using paste0() function in R.
Syntax for Paste() Function in R
Arguments
… | one or more R objects, to be concatenated together. |
sep | a character string to separate the terms. |
collapse | an optional character string to separate the results. |
Simple Paste() Function in R:
In its simplest usage, paste function in R will accept an unlimited number of scalars, and join them together, separating each scalar with a space by default
# paste function in R paste('one',2,'three',4,'five')
output:
When multiple arguments are passed to paste, it will vectorize the operation, recycling shorter elements when necessary. This makes it easy to generate variable names with a common prefix
# paste function in R with sep argument paste('X',1:5,sep='')
output:
Paste() function in R with collapse Argument:
When a vector is passed, collapse argument should be used as a separator
# paste function in R with collapse argument paste(c('one','two','three','four'),collapse=' and ')
output:
Paste() function in R with Sep & collapse Argument:
The sep= argument controls what is placed between each set of values that are combined, and the collapse= argument can be used to specify a value to use, when joining those individual values to create a single string.
# paste function in R with separator and collapse Argument paste(c('X','Y'),1:5,sep='_',collapse=' and ')
output:
paste() function of the dataframe:
Lets first create the dataframe as shown below
#### create dataframe in R df1 = data.frame ( state =c('Arizona','California','Texas','Las Vegas','Indiana','Alaska','New Jersey'), code = c('AZ','CL','TX','LV','ID','AK','NJ'), Hindex =c(62,-47,55,-74,31,-77,0))
so the dataframe will be
concatenate two column using paste() function
paste() function concatenates the “state” and “code” column with separator “-” as shown below.
### paste function of dataframe df1$state_code = paste(df1$state,df1$code,sep="-") df1
so the resultant concatenated dataframe will be
Paste0() Function in R
Paste0() is a simple paste() function with default separator “ ”. paste0() Function will take only two arguments as shown below
Arguments
… | one or more R objects, to be concatenated together. |
collapse | an optional character string to separate the results. |
# paste0() function in R paste0('X',1:5)
output:
Paste0() function in R with collapse Argument:
# paste0() function in R with collapse agrument paste0('X',1:5,collapse=",")
output:
paste0() function of the dataframe:
As we have already created the following dataframe
lets use paste0() function to concatenate two columns
concatenate two column using paste0() function
paste0() function concatenates the “state” and “code” column. paste0() function by default concatenates without any separator as shown below.
### paste0() function of dataframe df1$state_code = paste0(df1$state,df1$code) df1
so the resultant concatenated dataframe will be