How to use spread() method In R Language

The data transformation in the R spread method is used to spread any key-value pair in multiple columns in the data frame. It is used to increase the readability of the data specified in the data frame. The data is rearranged according to the list of columns in the spread() method. All the data in col2 is repeated until the values in col3 are exhausted. The entire data frame is returned as the output. It has the following syntax : 

Syntax: spread(col-name)

Parameter:

col-name – Name of one or more columns according to which data is to be structured.

The following code arranges the data such that the values in col2 are assigned as column headings and their corresponding values as cell values of the data frame :

R




# Importing tidyr
library(tidyr)
 
# Creating a data frame
data_frame = data.frame(
  col1 = c("A","A","A","A","A","A",
           "B","B","B","B","B","B"),
  col2 = c("Eng","Phy","Chem","MAQ","Bio","SST",
           "Eng","Phy","Chem","MAQ","Bio","SST"),
  col3 = c(34,56,46,23,72,67,89,43,88,45,78,99)
  )
 
print("Data Frame")
print(data_frame)
 
# Selecting values by col2 and col3
arr_data_frame <- data_frame %>%
    spread(col2,col3)
print("Spread using col2 and col3")
print(arr_data_frame)


Output:

   col1 col2 col3
1     A  Eng   34
2     A  Phy   56
3     A Chem   46
4     A  MAQ   23
5     A  Bio   72
6     A  SST   67
7     B  Eng   89
8     B  Phy   43
9     B Chem   88
10    B  MAQ   45
11    B  Bio   78
12    B  SST   99

[1] "Spread using col2 and col3"
  col1 Bio Chem Eng MAQ Phy SST
1    A  72   46  34  23  56  67
2    B  78   88  89  45  43  99

Explanation : 

The data in the data frame is spread using the col2 and col3 values. The col2 unique values are assigned as column headings, and their corresponding cell values are assigned in the data frame values. 

The following code arranges the data such that the values in col2 are assigned as row headings and their corresponding values as cell values of the data frame :

R




# Importing tidyr
library(tidyr)
 
# Creating a data frame
data_frame = data.frame(
  col1 = c("A","A","A","A","A","A",
           "B","B","B","B","B","B"),
  col2 = c("Eng","Phy","Chem","MAQ","Bio","SST",
           "Eng","Phy","Chem","MAQ","Bio","SST"),
  col3 = c(34,56,46,23,72,67,89,43,88,45,78,99)
)
 
print("Data Frame")
print(data_frame)
 
# Selecting values by col1 and col3
arr_data_frame <- data_frame %>%
    spread(col1,col3)
print("Spread using col1 and col3")
print(arr_data_frame)


Output:

   col1 col2 col3
1     A  Eng   34
2     A  Phy   56
3     A Chem   46
4     A  MAQ   23
5     A  Bio   72
6     A  SST   67
7     B  Eng   89
8     B  Phy   43
9     B Chem   88
10    B  MAQ   45
11    B  Bio   78
12    B  SST   99

[1] "Spread using col1 and col3"
  col2  A  B
1  Bio 72 78
2 Chem 46 88
3  Eng 34 89
4  MAQ 23 45
5  Phy 56 43
6  SST 67 99

How to Transform Data in R?

In this article, we will learn how to transform data in the R programming language.

Similar Reads

Data Transformation in R

The data transformation in R is mostly handled by the external packages tidyverse and dplyr . These packages provide many methods to carry out the data simulations. There are a large number of ways to simulate data transformation in R. These methods are widely available using these packages, which can be downloaded and installed using the following command :...

Method 1: Using Arrange() method

For data transformation in R, we will use The arrange() method, to create an order for the sequence of the observations given. It takes a single column or a set of columns as the input to the method and creates an order for these....

Method 2: Using select() method

...

Method 3: Using filter() method

...

Method 4: Using spread() method

Data transformation in R of the data frame can also be fetched using the select() method in tidyverse package. The columns are fetched in the order of their specification in the argument list of the select() method call. This method results in a subset of the data frame as the output. The following syntax is followed :...

Method 5: Using mutate() method

...

Method 6: Using group_by() and summarise() method

...

Method 7: Using the gather() method

The filter() method in the tidyverse package is used to apply a range of constraints and conditions to the column values of the data frame in data transformation in R. It filters the data and results in the smaller output returned by the column values satisfying the specified condition. The conditions are specified using the logical operators, and values are validated then.  A data frame can be supplied with the pipe operator and then using the filter condition....