How to Unnest dataframe in R ? - GeeksforGeeks

18 Jul, 2021
In this article, we will discuss how to unnest dataframes in R Programming Language. Unnesting of dataframe refers to flattening it.
The do.call() method in base R constructs and executes a function call from a function using its corresponding argument list. 
Syntax:
do.call(what, args)
Parameter : 
what – The name of the function to execute
args – Additional arguments to invoke the function upon. 
We invoke the function “data.frame” which transforms the object specified as the second argument into the data frame. The output is returned to the form of a data.frame object which contains the rows and corresponding columns’ information. 
Example:
R
# creating a data framedata_frame <- data.frame(col1 = sample(letters[1:5]),                         col2 = 1:5                         )  print ("Original DataFrame")print (data_frame)  # unnesting data frameunnest_df <- do.call(data.frame, data_frame)  # printing unnesting data framestr(unnest_df)
Output
[1] "Original DataFrame" 
col1 col2 
1    c    1 
2    e    2 
3    a    3 
4    b    4 
5    d    5
'data.frame': 5 obs. of  2 variables:  
$ col1: chr  "c" "e" "a" "b" ...  
$ col2: int  1 2 3 4 5
The purrr package in the R programming language is used to simulate easy working with functions as well as vectors. The bind_cols() method in R is used to bind columns of two or more data frames. The reduce() method is used to reduce a vector, x , to a single value by recursively calling a function. The reduce() method here is used to create second data frame object, which takes as input the last row of the data frame and uses “tibble” as the function. 
The output is obtained in the form of unnested data frame. 
Example:
R
library(purrr)  # creating a data framedata_frame <- data.frame(col1 = sample(letters[6:10]),                         col2 = 1:5                         )  print ("Original DataFrame")print (data_frame)  # unnesting data frameunnest_df <- bind_cols(data_frame[1], reduce(data_frame[-1], tibble))str(unnest_df)
Output
[1] "Original DataFrame" 
  col1 col2 
1    g    1 
2    i    2 
3    j    3 
4    h    4 
5    f    5 
'data.frame': 5 obs. of  2 variables:  
$ col1: chr  "g" "i" "j" "h" ...  
$ ...2: int  1 2 3 4 5
The tidyr package in R is used to “tidy” up the data. The unnest() method in the package can be used to convert the data frame into an unnested object by specifying the input data and its corresponding columns to use in unnesting. The output is produced in the form of a tibble in R. 
Syntax:
unnest (data, cols )
Parameters : 
data – The data frame to be unnested
cols – The columns to use for unnesting
Example:
R
library(tidyr)  # creating a data framedata_frame <- data.frame(col1 = sample(letters[6:10]),                         col2 = 1:5                         )  print ("Original DataFrame")print (data_frame)  # unnesting data frameunnest_df <- unnest(  data_frame , cols = c('col1','col2'))str(unnest_df)
Output
[1] "Original DataFrame" 
col1 col2 
1    i    1 
2    j    2 
3    g    3 
4    h    4 
5    f    5
tibble [5 × 2] (S3: tbl_df/tbl/data.frame)  
$ col1: chr [1:5] "i" "j" "g" "h" ...  
$ col2: int [1:5] 1 2 3 4 5
Picked
R DataFrame-Programs
R-DataFrame
R Language
R Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Change Color of Bars in Barchart using ggplot2 in R
Group by function in R using Dplyr
How to Change Axis Scales in R Plots?
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to Split Column Into Multiple Columns in R DataFrame?
Replace Specific Characters in String in R
How to filter R DataFrame by values in a column?
How to filter R dataframe by multiple conditions?
Convert Matrix to Dataframe in R