Author avatar

Deepika Singh

Reshaping Data with R

Deepika Singh

  • Nov 6, 2019
  • 8 Min read
  • 9 Views
  • Nov 6, 2019
  • 8 Min read
  • 9 Views
Data
R

Introduction

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.

Data

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.

1
2
3
4
5
6
7
8
9
10
11
df_wide <- read.table(header=TRUE, text='
     Rollno name   math science english
       1   Name1     80  72  77
       2   Name2     65  80  71
       3   Name3     45  54  67
       4   Name4     95  73  70
')

df_wide$Rollno <- factor(df_wide$Rollno) 

head(df_wide)
{r}

Output:

1
2
3
4
5
6
|   	| Rollno 	| name  	| math 	| science 	| english 	|
|---	|--------	|-------	|------	|---------	|---------	|
| 1 	| 1      	| Name1 	| 80   	| 72      	| 77      	|
| 2 	| 2      	| Name2 	| 65   	| 80      	| 71      	|
| 3 	| 3      	| Name3 	| 45   	| 54      	| 67      	|
| 4 	| 4      	| Name4 	| 95   	| 73      	| 70      	|

We’ll use this simple data to perform the reshaping tasks in the subsequent sections.

Using 'reshape2'

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.

Wide to Long Conversion

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:

  1. Argument 'id.vars': Specifies the variables to be retained and not split apart

  2. Argument 'measure.vars': Represents the source columns

  3. Argument 'variable.name': Represents the destination column that will identify the original variables from which the values came

  4. Argument 'value.name': Represents the values, which in our data are the exam marks

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.

1
2
3
4
5
6
7
library(reshape2)

df_long <- melt(df_wide, id.vars=c("Rollno", "name"), measure.vars=c("math", "science", "english" ), variable.name="subject", value.name="marks"
)

df_long
 
{r}

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
| Rollno  	| name  	| subject 	| marks 	|
|---------	|-------	|---------	|-------	|
| 1       	| Name1 	| math    	| 80    	|
| 2       	| Name2 	| math    	| 65    	|
| 3       	| Name3 	| math    	| 45    	|
| 4       	| Name4 	| math    	| 95    	|
| 1       	| Name1 	| science 	| 72    	|
| 2       	| Name2 	| science 	| 80    	|
| 3       	| Name3 	| science 	| 54    	|
| 4       	| Name4 	| science 	| 73    	|
| 1       	| Name1 	| english 	| 77    	|
| 2       	| Name2 	| english 	| 71    	|
| 3       	| Name3 	| english 	| 67    	|
| 4       	| Name4 	| english 	| 70    	|

Long to Wide Conversion

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.

1
2
data_wide <- dcast(df_long, Rollno + name ~ subject, value.var="marks")
data_wide
{r}

Output:

1
2
3
4
5
6
|   	| Rollno 	| name  	| math 	| science 	| english 	|
|---	|--------	|-------	|------	|---------	|---------	|
| 1 	| 1      	| Name1 	| 80   	| 72      	| 77      	|
| 2 	| 2      	| Name2 	| 65   	| 80      	| 71      	|
| 3 	| 3      	| Name3 	| 45   	| 54      	| 67      	|
| 4 	| 4      	| Name4 	| 95   	| 73      	| 70      	| 

Using 'tidyr'

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.

Wide to Long Conversion

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.

1
2
3
4
5
library(tidyr)

df_long_tidyr <- gather(df_wide, subject, marks, math:english, factor_key=TRUE)

df_long_tidyr
{r}

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
| Rollno  	| name  	| subject 	| marks 	|
|---------	|-------	|---------	|-------	|
| 1       	| Name1 	| math    	| 80    	|
| 2       	| Name2 	| math    	| 65    	|
| 3       	| Name3 	| math    	| 45    	|
| 4       	| Name4 	| math    	| 95    	|
| 1       	| Name1 	| science 	| 72    	|
| 2       	| Name2 	| science 	| 80    	|
| 3       	| Name3 	| science 	| 54    	|
| 4       	| Name4 	| science 	| 73    	|
| 1       	| Name1 	| english 	| 77    	|
| 2       	| Name2 	| english 	| 71    	|
| 3       	| Name3 	| english 	| 67    	|
| 4       	| Name4 	| english 	| 70    	|

Long to Wide Conversion

The lines of code below return the original data, which was in wide format, by using the 'spread' function.

1
2
df_wide_tidyr <- spread(df_long_tidyr, subject, marks)
df_wide_tidyr
{r}

Output:

1
2
3
4
5
6
|   	| Rollno 	| name  	| math 	| science 	| english 	|
|---	|--------	|-------	|------	|---------	|---------	|
| 1 	| 1      	| Name1 	| 80   	| 72      	| 77      	|
| 2 	| 2      	| Name2 	| 65   	| 80      	| 71      	|
| 3 	| 3      	| Name3 	| 45   	| 54      	| 67      	|
| 4 	| 4      	| Name4 	| 95   	| 73      	| 70      	|

Transposing the Data

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.

1
t(df_wide)
{r}

Output:

1
2
3
4
5
6
        [,1]    [,2]    [,3]    [,4]   
Rollno  "1"     "2"     "3"     "4"    
name    "Name1" "Name2" "Name3" "Name4"
math    "80"    "65"    "45"    "95"   
science "72"    "80"    "54"    "73"   
english "77"    "71"    "67"    "70"   

Conclusion

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:

0