Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Hypothesis Testing in R
    Hypothesis Testing in R R
  • OLS Regression in R
    OLS Regression in R R
  • Create new variables from existing variables in R
    Create new variables from existing variables in R R
  • How to Replace Inf Values with NA in R
    How to Replace Inf Values with NA in R R
  • What is bias variance tradeoff
    What is the bias variance tradeoff? R
  • Replace NA with Zero in R
    Replace NA with Zero in R R
  • How Do Online Criminals Acquire Sensitive Data
    How Do Online Criminals Acquire Sensitive Data Machine Learning
  • best books about data analytics
    Best Books to Learn Statistics for Data Science Course
How to Use Spread Function in R

How to Use Spread Function in R?-tidyr Part1

Posted on July 19July 17 By Jim No Comments on How to Use Spread Function in R?-tidyr Part1
Tweet
Share
Share
Pin

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. 

Tweet
Share
Share
Pin
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 Find Optimal Clusters in R, K-means clustering is one of the most widely used clustering techniques in machine learning.
    How to Find Optimal Clusters in R? R
  • How to Calculate Lag by Group in R
    How to Calculate Lag by Group in R? R
  • How to Group and Summarize Data in R
    How to Group and Summarize Data in R R
  • Dynamic data visualizations in R
    Dynamic data visualizations in R R
  • Two-Way ANOVA Example in R
    How to perform a one-sample t-test in R? R
  • Methods for Integrating R and Hadoop
    Methods for Integrating R and Hadoop complete Guide R

Leave a Reply Cancel reply

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

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Tips for Data Scientist Interview Openings
  • What is Epoch in Machine Learning?
  • Dynamic data visualizations in R
  • How Do Machine Learning Chatbots Work
  • Convex optimization role in machine learning

Check your inbox or spam folder to confirm your subscription.

  • Sampling from the population in R
  • Two of the Best Online Data Science Courses for 2023
  • Process of Machine Learning Optimisation?
  • ggplot2 scale in R (grammar for graphics)
  • ggplot aesthetics in R (Grammer of graphics)
  • gganatogram Plot in R
    How to create Anatogram plot in R R
  • R Percentage by Group Calculation
    R Percentage by Group Calculation R
  • How to compare variances in R
    How to compare variances in R R
  • Top 10 Data Visualisation Tools
    Top 10 Data Visualisation Tools Every Data Science Enthusiast Must Know Course
  • Subsetting with multiple conditions in R
    Subsetting with multiple conditions in R R
  • How to Analyze Likert Scale Data
    How to Analyze Likert Scale Data? Statistics
  • Error: Can't rename columns that don't exist
    Can’t rename columns that don’t exist R
  • Is R or Python Better for Data Science in Bangalore
    Is R or Python Better for Data Science in Bangalore R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme