How to Unnest dataframe in R ?
Last Updated :
18 Jul, 2021
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
Similar Reads
How to split DataFrame in R In this article, we will discuss how to split the dataframe in R programming language. A subset can be split both continuously as well as randomly based on rows and columns. The rows and columns of the dataframe can be referenced using the indexes as well as names. Multiple rows and columns can be r
4 min read
How to Remove Rows in R DataFrame? In this article, we will discuss how to remove rows from dataframe in the R programming language. Method 1: Remove Rows by Number By using a particular row index number we can remove the rows. Syntax: data[-c(row_number), ] where. data is the input dataframerow_number is the row index position Exam
2 min read
How to Delete Row(s) in R DataFrame ? In this article, we will see how row(s) can be deleted from a Dataframe in R Programming Language. Deleting a single row For this, the index of the row to be deleted is passed with a minus sign. Syntax: df[-(index), ] Example 1 :Â R # creating a data frame with # some data . df=data.frame(id=c(1,2,3
2 min read
How to add a row to R dataframe ? In this article, we will see how to add rows to a DataFrame in R Programming Language. To do this we will use rbind() function. This function in R Language is used to combine specified Vector, Matrix or Data Frame by rows. Syntax: rbind(dataframe 1, dataframe 2) Example 1 : R # creating a data frame
2 min read
How to add dataframe to dataframe in R ? In this article, we will see how to add dataframe at the end of another dataframe in R Programming Language. Method 1: Using rbind() method The rbind() method in R works only if both the input dataframe contains the same columns with similar lengths and names. The dataframes may have a different num
4 min read