Open In App

How to Unnest dataframe in R ?

Last Updated : 18 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to unnest dataframes in R Programming Language. Unnesting of dataframe refers to flattening it.

Method 1: Using do.call approach 

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 frame
data_frame <- data.frame(col1 = sample(letters[1:5]),
                         col2 = 1:5
                         )
  
print ("Original DataFrame")
print (data_frame)
  
# unnesting data frame
unnest_df <- do.call(data.frame, data_frame)
  
# printing unnesting data frame
str(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

Method 2: Using purrr package

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 frame
data_frame <- data.frame(col1 = sample(letters[6:10]),
                         col2 = 1:5
                         )
  
print ("Original DataFrame")
print (data_frame)
  
# unnesting data frame
unnest_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

Method 3: Using tidyr package

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 frame
data_frame <- data.frame(col1 = sample(letters[6:10]),
                         col2 = 1:5
                         )
  
print ("Original DataFrame")
print (data_frame)
  
# unnesting data frame
unnest_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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads