Skip to content

Data Science Tutorials

For Data Science Learners

  • Descriptive Statistics in R R
  • Cross-validation in Machine Learning
    Cross-validation in Machine Learning Statistics
  • How to Calculate Relative Frequencies in R
    How to Calculate Relative Frequencies in R? R
  • Tips for Rearranging Columns in R
    Tips for Rearranging Columns in R R
  • Split a Vector into Chunks in R R
  • Convert a continuous variable to a categorical in R R
  • Two-Way ANOVA Example in R
    How to perform One-Sample Wilcoxon Signed Rank Test in R? R
  • Add Footnote to ggplot2 R
How to Use Spread Function in R

How to Use Spread Function in R?-tidyr Part1

Posted on July 19July 17 By Admin No Comments on How to Use Spread Function in R?-tidyr Part1

How to Use Spread Function in R, To “spread” a key-value pair across multiple columns, use the spread() method from the tidyr package.

The basic syntax used by this function is as follows.

spread(data, key value)

where:

data: Name of the data frame

key: column whose values will serve as the names of variables

value: Column where new variables formed from keys will populate with values

How to Use Spread Function in R?

The practical application of this function is demonstrated in the examples that follow.

dplyr Techniques and Tips – Data Science Tutorials

Example 1: Divide Values Between Two Columns

Let’s say we have the R data frame shown below.

Let’s create a data frame

df <- data.frame(player=rep(c('A', 'B'), each=4),
year=rep(c(1, 1, 2, 2), times=2),
stat=rep(c('points', 'assists'), times=4),
amount=c(14, 6, 18, 7, 22, 9, 38, 4))

Now we can view the data frame

df
   player year    stat amount
1     P1    1  points    125
2     P1    1 assists    142
3     P1    2  points    145
4     P1    2 assists    157
5     P2    1  points    134
6     P2    1 assists    213
7     P2    2  points    125
8     P2    2 assists    214

The stat column’s values can be separated into separate columns using the spread() function.

library(tidyr)

Dividing the stats column into several columns

spread(df, key=stat, value=amount)
player year assists points
1     P1    1     142    125
2     P1    2     157    145
3     P2    1     213    134
4     P2    2     214    125

Example 2: Values Should Be Spread Across More Than Two Columns

Let’s say we have the R data frame shown below:

Imagine we have the following data frame

df2 <- data.frame(player=rep(c('P1'), times=8),
year=rep(c(1, 2), each=4),
stat=rep(c('points', 'assists', 'steals', 'blocks'), times=2),
amount=c(115, 116, 212, 211, 229, 319, 213, 314))

Now we can view the data frame

df2
  player year    stat amount
1     P1    1  points    115
2     P1    1 assists    116
3     P1    1  steals    212
4     P1    1  blocks    211
5     P1    2  points    229
6     P1    2 assists    319
7     P1    2  steals    213
8     P1    2  blocks    314

The spread() function can be used to create four additional columns from the stat column’s four distinct values.

library(tidyr)

Dividing the stats column into several columns

spread(df2, key=stat, value=amount)
   player year assists blocks points steals
1     P1    1     116    211    115    212
2     P1    2     319    314    229    213

Check your inbox or spam folder to confirm your subscription.

How to Group and Summarize Data in R – Data Science Tutorials

Have you liked this article? If you could email it to a friend or share it on Facebook, Twitter, or Linked In, I would be eternally grateful.

Please use the like buttons below to show your support. Please remember to share and comment below. 

R Tags:tidyr

Post navigation

Previous Post: Add new calculated variables to a data frame and drop all existing variables
Next Post: How to Use Gather Function in R?-tidyr Part2

Related Posts

  • How to Recode Values in R
    How to Recode Values in R R
  • Determine the significance of a mediation effect in R
    Determine the significance of a mediation effect in R R
  • What is bias variance tradeoff
    What is the bias variance tradeoff? R
  • How to test the significance of a mediation effect
    How to test the significance of a mediation effect R
  • Box Cox transformation in R
    Box Cox transformation in R R
  • Group By Minimum in R
    Group By Minimum in R R

Leave a Reply Cancel reply

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

  • Maximizing Model Accuracy with Train-Test Splits in Machine Learning
  • Type II Errors in R
  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • Data Science Strategies for Improving Customer Experience in R
    Data Science Strategies for Improving Customer Experience in R R
  • Correlation By Group in R R
  • How to Create a Covariance Matrix in R
    How to Create a Covariance Matrix in R? R
  • How to Use Gather Function in R
    How to Use Gather Function in R?-tidyr Part2 R
  • Data Analytics Online Courses for Beginners
    Data Analytics Online Courses for Beginners Course
  • Remove Columns from a data frame
    How to Remove Columns from a data frame in R R
  • one-sample-proportion-test-in-r
    One sample proportion test in R-Complete Guide R
  • Calculating Z-Scores in R: A Step-by-Step Guide R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme