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.
Contact Us