The spread() Function In R: How To Use spread() In R

spread() function in r

In this tutorial, we will show you what the spread() function in R is and how to use it. The spread() function is often used to reformat a data frame from long format to short format. Check out the instructions below for more details.

Spread() function in R

What does the spread() function do in R?

Let’s take a look at this example: 

We have this dataset: 

    	Group 		Vehicles 	Amount
1 	Group 1      	car             1
2 	Group 2     	bike            3
3 	Group 3     	bike            2
4 	Group 2      	car             2
5 	Group 1     	bike            4
6 	Group 3      	car             3

We want to count each type of vehicle in each group. However, the current table is quite hard to do that. So instead, we will take each type of vehicles as new columns like this: 

    	Group 		bike 	car
1 	Group 1    	4   	1
2 	Group 2    	3   	2
3 	Group 3    	2   	3

And we do this by using the spread() function. The spread() function will spread a key-value pair across multiple columns, or we can say that it reshapes our data frame from long to short.

Syntax:

spread(df, key, value)

Parameters:

  • df: the data frame which we want to change.
  • key: the columns in which its values are the new columns’ names.
  • value: the columns in which its values will be used to fill in the new columns created by the new keys. Or, in short, the values of the new keys. 

How to use spread() in R

Let’s say we have this data frame that represents the number of 3 different kinds of fruits that each class as a school needs for their students. 

dataFrame <- data.frame(
    class = c("Class A", "Class B", "Class C", "Class D",
        "Class B", "Class A", "Class D", "Class C",
        "Class D", "Class A", "Class B", "Class C"),
    fruit = c("apple", "orange", "apple", "pea", "pea", "orange",
        "apple", "pea", "orange", "pea", "apple", "orange"), 
    quantity = c(15, 23, 19, 17, 21, 20, 21, 14, 24, 19, 18, 20)
)

dataFrame

Our data frame:

     class  fruit quantity
1  Class A  apple       15
2  Class B orange       23
3  Class C  apple       19
4  Class D    pea       17
5  Class B    pea       21
6  Class A orange       20
7  Class D  apple       21
8  Class C    pea       14
9  Class D orange       24
10 Class A    pea       19
11 Class B  apple       18
12 Class C orange       20

However, this table is not really suitable for this kind of data and thus hard to follow. Instead, we will use the spread() function to make key-value columns become a set of columns.

To use the spread() function in R, first, we have to install the ‘tidyr‘ library.

Type in the console: 

install.packages("tidyr")

Then load the library using:

library(tidyr)

Now we can use the spread() function in our program. In our data frame, we will want the key to be the 3 types of fruit and the value is the quantity, so key=fruit and value=quantity.

spread(dataFrame, key = fruit, value = quantity)

Completed code:

dataFrame <- data.frame(
    class = c("Class A", "Class B", "Class C", "Class D", 
        "Class B", "Class A", "Class D", "Class C", 
        "Class D", "Class A", "Class B", "Class C"),
    fruit = c("apple", "orange", "apple", "pea", "pea", "orange",
        "apple", "pea", "orange", "pea", "apple", "orange"),
    quantity = c(15, 23, 19, 17, 21, 20, 21, 14, 24, 19, 18, 20)
)
    
library(tidyr)

spread(dataFrame, key = fruit, value = quantity)

Output:

    class apple orange pea
1 Class A    15     20  19
2 Class B    18     23  21
3 Class C    19     20  14
4 Class D    21     24  17

As you can see, this new table is much more readable and easy to count how many of each kind of fruits each class needs. 

Summary

In this tutorial, we have shown you the definition of the spread() function in R and its applications. This is a really useful function if you want to use the values of a column to create other columns and make the data frame shorter and wider.

Maybe you are interested:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *