In predictive modeling, it is often necessary to reshape the data to make it ready for conducting analysis or building models. The process of transforming the data into a clear, simple, and desirable form is an integral component of data science. The most common reshaping process is converting the data from wide to long format and vice versa.
In this guide, you will learn about techniques for reshaping data in R. There are many powerful libraries in R to perform this task, and you will learn about two such packages: reshape2 and tidyr.
In this guide, we will use simple fictitious data of grades (out of 100) for five students in three subjects. Let's start by creating sample data using the lines of code below. We will also convert the roll number to a factor variable, as this will help in data wrangling.
1df_wide <- read.table(header=TRUE, text='
2 Rollno name math science english
3 1 Name1 80 72 77
4 2 Name2 65 80 71
5 3 Name3 45 54 67
6 4 Name4 95 73 70
7')
8
9df_wide$Rollno <- factor(df_wide$Rollno)
10
11head(df_wide)
Output:
1| | Rollno | name | math | science | english |
2|--- |-------- |------- |------ |--------- |--------- |
3| 1 | 1 | Name1 | 80 | 72 | 77 |
4| 2 | 2 | Name2 | 65 | 80 | 71 |
5| 3 | 3 | Name3 | 45 | 54 | 67 |
6| 4 | 4 | Name4 | 95 | 73 | 70 |
We’ll use this simple data to perform the reshaping tasks in the subsequent sections.
reshape2 is an R package written by Hadley Wickham that makes it easy to transform data between the wide and the long formats. Wide data has a column for every variable, while this is not compulsory in long data.
Sometimes you may be required to reshape wide data into a long format. The melt function in 'reshape2' performs this task.
The first line of code below loads the library, while the second line melts the data into the long format. The arguments to be specified are outlined below:
Argument 'id.vars': Specifies the variables to be retained and not split apart
Argument 'measure.vars': Represents the source columns
Argument 'variable.name': Represents the destination column that will identify the original variables from which the values came
The resultant data is stored in a new data frame, 'df_long'. The third line of code below prints this resulting data set, which now contains 12 observations of 4 variables.
1library(reshape2)
2
3df_long <- melt(df_wide, id.vars=c("Rollno", "name"), measure.vars=c("math", "science", "english" ), variable.name="subject", value.name="marks"
4)
5
6df_long
7
Output:
1| Rollno | name | subject | marks |
2|--------- |------- |--------- |------- |
3| 1 | Name1 | math | 80 |
4| 2 | Name2 | math | 65 |
5| 3 | Name3 | math | 45 |
6| 4 | Name4 | math | 95 |
7| 1 | Name1 | science | 72 |
8| 2 | Name2 | science | 80 |
9| 3 | Name3 | science | 54 |
10| 4 | Name4 | science | 73 |
11| 1 | Name1 | english | 77 |
12| 2 | Name2 | english | 71 |
13| 3 | Name3 | english | 67 |
14| 4 | Name4 | english | 70 |
In the previous section, we created long data from wide data. To convert long data back into a wide format, we can use the cast function. There are many cast functions, but we will use the dcast function because it is used for data frames.
The lines of code below will perform this conversion.
1data_wide <- dcast(df_long, Rollno + name ~ subject, value.var="marks")
2data_wide
Output:
1| | Rollno | name | math | science | english |
2|--- |-------- |------- |------ |--------- |--------- |
3| 1 | 1 | Name1 | 80 | 72 | 77 |
4| 2 | 2 | Name2 | 65 | 80 | 71 |
5| 3 | 3 | Name3 | 45 | 54 | 67 |
6| 4 | 4 | Name4 | 95 | 73 | 70 |
The tidyr package can also be used to reshape data. It uses the gather function to convert data from wide to long format and uses the spread function to convert it from long to wide format.
The first line of code below loads the library, while the second line reshapes the data to long format. The third line prints the resultant data.
1library(tidyr)
2
3df_long_tidyr <- gather(df_wide, subject, marks, math:english, factor_key=TRUE)
4
5df_long_tidyr
Output:
1| Rollno | name | subject | marks |
2|--------- |------- |--------- |------- |
3| 1 | Name1 | math | 80 |
4| 2 | Name2 | math | 65 |
5| 3 | Name3 | math | 45 |
6| 4 | Name4 | math | 95 |
7| 1 | Name1 | science | 72 |
8| 2 | Name2 | science | 80 |
9| 3 | Name3 | science | 54 |
10| 4 | Name4 | science | 73 |
11| 1 | Name1 | english | 77 |
12| 2 | Name2 | english | 71 |
13| 3 | Name3 | english | 67 |
14| 4 | Name4 | english | 70 |
The lines of code below return the original data, which was in wide format, by using the 'spread' function.
1df_wide_tidyr <- spread(df_long_tidyr, subject, marks)
2df_wide_tidyr
Output:
1| | Rollno | name | math | science | english |
2|--- |-------- |------- |------ |--------- |--------- |
3| 1 | 1 | Name1 | 80 | 72 | 77 |
4| 2 | 2 | Name2 | 65 | 80 | 71 |
5| 3 | 3 | Name3 | 45 | 54 | 67 |
6| 4 | 4 | Name4 | 95 | 73 | 70 |
The transpose function reverses rows into columns and vice versa. It is perhaps the simplest method of reshaping a dataset, and it uses the t() function to transpose a matrix or a data frame, as shown below.
1t(df_wide)
Output:
1 [,1] [,2] [,3] [,4]
2Rollno "1" "2" "3" "4"
3name "Name1" "Name2" "Name3" "Name4"
4math "80" "65" "45" "95"
5science "72" "80" "54" "73"
6english "77" "71" "67" "70"
In this guide, you have learned two popular data reshaping techniques using the 'reshape2' and 'tidyr' packages. You also learned about transposing data. These tools will help you convert your data into a format that is easier to analyze. To learn more about data science in R, please refer to the following guides: