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
Reshape DataFrame from Long to Wide Format in R
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.