Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • What Is the Best Way to Filter by Date in R
    What Is the Best Way to Filter by Date in R? R
  • pheatmap function in R
    The pheatmap function in R R
  • Methods for Integrating R and Hadoop
    Methods for Integrating R and Hadoop complete Guide R
  • Get the first value in each group in R
    Get the first value in each group in R? R
  • How to compare the performance of different algorithms in R
    How to compare the performance of different algorithms in R? R
  • similarity measure between two populations
    Similarity Measure Between Two Populations-Brunner Munzel Test Statistics
  • Two Sample Proportions test in R
    Two Sample Proportions test in R-Complete Guide R
  • Hypothesis Testing Examples
    Hypothesis Testing Examples-Quick Overview Statistics
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

  • How to put margins on tables or arrays in R?
    How to put margins on tables or arrays in R? R
  • How to convert characters from upper to lower case in R
    How to convert characters from upper to lower case in R? R
  • Dealing Missing values in R
    Dealing With Missing values in R R
  • Methods for Integrating R and Hadoop
    Methods for Integrating R and Hadoop complete Guide R
  • How do augmented analytics work
    How do augmented analytics work? R
  • How to create a ggalluvial plot in r
    How to create a ggalluvial plot in R? 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
  • Top 7 Skills Required to Become a Data Scientist
  • Learn Hadoop for Data Science
  • How Do Online Criminals Acquire Sensitive Data
  • Top Reasons To Learn R in 2023
  • Linear Interpolation in R-approx

Check your inbox or spam folder to confirm your subscription.

 https://www.r-bloggers.com
  • ggdogs on ggplot2
    ggdogs on ggplot2 R
  • How to move from Junior Data Scientist
    How to move from Junior Data Scientist Machine Learning
  • Best Data Science YouTube Tutorials
    Best Data Science YouTube Tutorials Free to Learn Course
  • Hypothesis Testing in R
    Hypothesis Testing in R R
  • Two-Way ANOVA Example in R
    Two-Way ANOVA Example in R-Quick Guide R
  • How to add columns to a data frame in R
    How to add columns to a data frame in R R
  • Select the First Row by Group in R
    Select the First Row by Group in R R
  • Algorithm Classifications in Machine Learning
    Algorithm Classifications in Machine Learning Machine Learning

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme