How to use do.call method In R Language

The strsplit() method in R is used to split the specified column string vector into corresponding parts. The pattern is used to divide the string into subparts. 

Syntax:

strsplit(str, pattern)

Parameter :

  • str: The string vector to be split.
  • pattern: Pattern to split up the string by.

The do.call() method is used to call a function from within a method name. The rbind() method can then be used to combine the columns obtained as vectors as a result of the application of strsplit method. 

Syntax:

do.call(what, args)

Parameter: 

  • what – The function to execute
  • args – Additional arguments to execute.

Example: Split Dataframe variable into multiple columns

R




# creating a dataframe
data_frame <- data.frame(
                         col1 = c("val_1","val_2","val_3","val_4")
                         )
print("Original DataFrame")
print(data_frame)
 
# splitting values in column
print("Modified DataFrame")
 
# splitting the values of col1 using underscore character
data.frame(do.call("rbind", strsplit(as.character(data_frame$col1), "_",
                                     fixed = TRUE)))


Output:

[1] "Original DataFrame"
  col1
1 val_1
2 val_2
3 val_3
4 val_4
[1] "Modified DataFrame"
  X1 X2
1 val  1
2 val  2
3 val  3
4 val  4

Split DataFrame Variable into Multiple Columns in R

In this article, we will discuss how to split dataframe variables into multiple columns using R programming language.

Similar Reads

Method 1: Using do.call method

The strsplit() method in R is used to split the specified column string vector into corresponding parts. The pattern is used to divide the string into subparts....

Method 2: Using tidyr package

...

Method 3: Using stringr package

The tidyr package in R is used to mutate and visualize the data. It is used to tidy up the data. The package can be downloaded and installed into the working space using the following command:...