Skip to content

Data Science Tutorials

For Data Science Learners

  • How to Calculate Relative Frequencies in R
    How to Calculate Relative Frequencies in R? R
  • Export output as text in R R
  • Add Significance Level and Stars to Plot in R
    Add Significance Level and Stars to Plot in R R
  • How to Use “not in” operator in Filter
    How to Use “not in” operator in Filter R
  • How to Add a title to ggplot2 Plots in R
    How to Add a caption to ggplot2 Plots in R? R
  • How to add Axes to Plot in R R
  • How to do Conditional Mutate in R
    How to do Conditional Mutate in R? R
  • How to Rank by Group in R?
    How to Rank by Group 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 Admin No Comments on How to Create a Frequency Table by Group in R?

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

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

  • Jarque-Bera Test in R
    Jarque-Bera Test in R With Examples R
  • Two Sample Proportions test in R
    Two Sample Proportions test in R-Complete Guide R
  • Sort or Order Rank in R R
  • How to Calculate Relative Frequencies in R
    How to Calculate Relative Frequencies in R? R
  • How to create a heatmap in R
    How to create a heatmap in R R
  • glm function in R
    glm function in r-Generalized Linear Models 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.

  • How to Create an Interaction Plot in R
    How to Create an Interaction Plot in R? R
  • How to Specify Histogram Breaks in R R
  • Data Science Challenges in R Programming Language
    Data Science Challenges in R Programming Language Machine Learning
  • Convex optimization role in machine learning
    Convex optimization role in machine learning Machine Learning
  • How to Join Multiple Data Frames in R
    How to Join Multiple Data Frames in R R
  • Subsetting with multiple conditions in R
    Subsetting with multiple conditions in R R
  • How to create contingency tables in R
    How to create contingency tables in R? R
  • How to test the significance of a mediation effect
    How to test the significance of a mediation effect R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme