Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • Defensive Programming Strategies in R
    Defensive Programming Strategies in R Machine Learning
  • rejection region in hypothesis testing
    Rejection Region in Hypothesis Testing Statistics
  • Two-Way ANOVA Example in R
    How to perform a one-sample t-test in R? R
  • best books about data analytics
    Best Books to learn Tensorflow Course
  • How to apply a transformation to multiple columns in R?
    How to apply a transformation to multiple columns in R? R
  • Add Significance Level and Stars to Plot in R
    Add Significance Level and Stars to Plot in R R
  • Is Data Science a Dying Profession
    Is Data Science a Dying Profession? R
  • How to Join Data Frames for different column names in R
    How to Join Data Frames for different column names in R R
How to Create a Frequency Table by Group in R

How to Create a Frequency Table by Group in R?

Posted on July 8July 8 By Jim No Comments on How to Create a Frequency Table by Group in R?
Tweet
Share
Share
Pin

How to Create a Frequency Table by Group in R?, To produce a frequency table by the group in R, use the dplyr package’s following functions.

Reorder Boxplots in R with Examples »

How to Create a Frequency Table by Group in R

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

df <- data.frame(team=c('P1', 'P1', 'P1', 'P2', 'P2', 'P3', 'P3', 'P3'),
                 position=c('R1', 'R3', 'R2', 'R2', 'R1', 'R1', 'R2', 'R1'))

Now we can view the data frame

df
  team position
1   P1       R1
2   P1       R3
3   P1       R2
4   P2       R2
5   P2       R1
6   P3       R1
7   P3       R2
8   P3       R1

Let’s say that we want to make a table of frequencies that lists the frequency of each position, sorted by team.

To do this, we can use the syntax shown below.

library(dplyr)

compute position frequency, grouped by team.

How to create contingency tables in R? – Data Science Tutorials

df %>%
  group_by(team, position) %>%
  summarize(Freq=n())
team  position  Freq
  <chr> <chr>    <int>
1 P1    R1           1
2 P1    R2           1
3 P1    R3           1
4 P2    R1           1
5 P2    R2           1
6 P3    R1           2
7 P3    R2           1

How to interpret the result is as follows:

1 player on team P1 has position ‘R1’

1 player on team P1 have position ‘R2’

And so on…

Keep in mind that by modifying the variable name in the summarise() method, we can rename the column that contains the frequencies.

We could, for instance, change the column’s name to “count” instead.

Best Books to Learn R Programming – Data Science Tutorials

library(dplyr)

compute position frequency, grouped by team.

df %>%
  group_by(team, position) %>%
  summarize(count=n())
team  position count
  <chr> <chr>    <int>
1 P1    R1           1
2 P1    R2           1
3 P1    R3           1
4 P2    R1           1
5 P2    R2           1
6 P3    R1           2
7 P3    R2           1

Check your inbox or spam folder to confirm your subscription.

How to Replace String in Column using R – Data Science Tutorials

Tweet
Share
Share
Pin
R Tags:dplyr

Post navigation

Previous Post: How to Calculate Relative Frequencies in R?
Next Post: Select the First Row by Group in R

Related Posts

  • pheatmap function in R
    The pheatmap function in R R
  • how to draw heatmap in r
    How to draw heatmap in r: Quick and Easy way R
  • Rounded corner bar plot in R
    How to make a rounded corner bar plot in R? R
  • Subset rows based on their integer locations
    Subset rows based on their integer locations-slice in R R
  • Checking Missing Values in R
    Checking Missing Values in R R
  • How to Use Spread Function in R
    How to Use Spread Function in R?-tidyr Part1 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)
  • How to handle Imbalanced Data
    How to handle Imbalanced Data? R
  • How to Filter Rows In R
    How to Filter Rows In R? R
  • How to Calculate Relative Frequencies in R
    How to Calculate Relative Frequencies in R? R
  • How to Join Multiple Data Frames in R
    How to Join Multiple Data Frames in R R
  • How to Turn Off Scientific Notation in R
    How to Turn Off Scientific Notation in R? R
  • Dynamic data visualizations in R
    Dynamic data visualizations in R R
  • How to Find Quartiles in R
    How to Find Quartiles in R? R
  • Top Data Science Skills
    Top Data Science Skills- step by step guide Machine Learning

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme