Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Reshape DataFrame from Long to Wide Format in R

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article, we will discuss how to reshape dataframe from long to wide format in R programming language.

The data in the dataframe is recognized by the format in which the storage and retrieval happens. Duplicate key-value pairs exist in dataframe and can be rearranged using the following methods.

Method 1 : Using reshape() method

The reshape method in R is used to reshape the grouped data. It is available in base R. The method basically reshapes a specified dataframe between different formats, that is ‘wide’ format with repetitive measurements done in separate columns as well as ‘long’ formats with repetitive measurements in separate rows. 

Syntax: reshape (data-frame, idvar = , timevar = , direction = )

Parameter : 

  • data-frame : The dataframe to reshape
  • timevar : Indicator of the variable given in long format that differentiates multiple records from the same group. 
  • idvar : Indicator of the variable(s) in long format that identify multiple records from the same group. 
  • direction : specifies whether to depict data in “long” or “wide” format. 

The output retains the original dataframe row numbers. The missing values are appended with an NA value in the dataframe. 

Example: Reshape dataframe from long to wide

R




# create first dataframe
data_frame1<-data.frame(col1=c(rep('Grp1',2),
                               rep('Grp2',2),
                               rep('Grp3',2)), 
                        col2=rep(1:3,2),
                        col3 = letters[1:6]
                        )
  
print("Original DataFrame")
print(data_frame1)
  
# reshaping the data
data_frame_mod <- reshape(data_frame1, idvar = "col1"
                          timevar = "col2", direction = "wide")
  
print("Modified DataFrame")
print(data_frame_mod)

Output:

[1] "Original DataFrame" 
  col1 col2 col3 
1 Grp1    1    a 
2 Grp1    2    b 
3 Grp2    3    c 
4 Grp2    1    d 
5 Grp3    2    e 
6 Grp3    3    f 
[1] "Modified DataFrame" 
  col1 col3.1 col3.2 col3.3 
1 Grp1      a      b   <NA> 
3 Grp2      d   <NA>      c 
5 Grp3   <NA>      e      f

Method 2 : Using tidyr package

The package can be downloaded and installed into the working space using the following syntax : 

install.packages("tidyr")

The spread method of this package can be used to reshape the data from long to wide format in R. The method is used to append a new column for each unique value of the key column. These unique set of values will form the names for new columns.

Syntax:

spread (data-frame, key, value)

Example: Reshape dataframe from long to wide

Python3




library("tidyr")
  
# create first dataframe
data_frame1 < -data.frame(col1=c(rep('Grp1', 2), rep('Grp2', 2), rep('Grp3', 2)),
                          col2=rep(1: 3, 2),
                          col3=letters[1:6]
                          )
print("Original DataFrame")
print(data_frame1)
  
# reshaping the data
data_frame_mod < -  spread(data_frame1,
                           key=col2,
                           value=col3)
print("Modified DataFrame")
print(data_frame_mod)

Output:

 col1    1    2    3 
1 Grp1    a    b <NA> 
2 Grp2    d <NA>    c 
3 Grp3 <NA>    e    f

My Personal Notes arrow_drop_up
Last Updated : 23 Sep, 2021
Like Article
Save Article
Similar Reads
Related Tutorials