Paste Function in R – paste0()

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

paste (…, sep = ” “, collapse = NULL)
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:
[1] “one 2 three 4 five”

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:
[1] “X1” “X2” “X3” “X4” “X5”

 

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:
“one and two and three and four

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:
[1] “X_1 and Y_2 and X_3 and Y_4 and X_5”

 

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

paste() function in R - paste0() 0

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

paste() function in R - paste0() 2

 

 

 

Paste0() Function in R

Paste0()  is a simple paste() function with default separator “ ”. paste0() Function will take only two arguments as shown below

paste0(…, collapse = NULL)
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:
[1] “X1” “X2” “X3” “X4” “X5”

 

Paste0() function in R with collapse Argument:

# paste0() function in R with collapse agrument

paste0('X',1:5,collapse=",")
output:
[1] “X1,X2,X3,X4,X5”

 

paste0() function of the dataframe:

As we have already created the following dataframe

paste() function in R - paste0() 0

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

paste() function in R - paste0() 1

previous small paste function in r                                                                                                         next smalle paste function in r