Skip to content

Data Science Tutorials

For Data Science Learners

  • How to move from Junior Data Scientist
    How to move from Junior Data Scientist Machine Learning
  • How to Compare Two Lists in Excel Using VLOOKUP
    How to Compare Two Lists in Excel Using VLOOKUP Excel
  • learn Hadoop for Data Science
    Learn Hadoop for Data Science Machine Learning
  • Filter Using Multiple Conditions in R
    Filter Using Multiple Conditions in R R
  • Two-Way ANOVA Example in R
    How to perform a one-sample t-test in R? R
  • stacked barplot in R
    Stacked Barplot in R R
  • Checking Missing Values in R
    Checking Missing Values in R R
  • How to Avoid Overfitting
    How to Avoid Overfitting? Machine Learning
Crosstab calculation in R

Crosstab calculation in R

Posted on June 11June 11 By Admin No Comments on Crosstab calculation in R

Crosstab calculation in R, To create a crosstab using functions from the dplyr and tidyr packages in R, use the following basic syntax.

df %>%
  group_by(var1, var2) %>%
  tally() %>%
  spread(var1, n)

The examples below demonstrate how to utilize this syntax in practice.

Control Chart in Quality Control-Quick Guide – Data Science Tutorials

Example 1: Make a simple crosstab

Let’s say we have the following R data frame:

Let’s create a data frame

df <- data.frame(team=c('X', 'X', 'X', 'X', 'Y', 'Y', 'Y', 'Y'),
                 position=c('A', 'A', 'B', 'C', 'C', 'C', 'D', 'D'),
                 points=c(107, 207, 208, 211, 213, 215, 219, 313))

Now we can view the data frame

df
   team position points
1    X        A    107
2    X        A    207
3    X        B    208
4    X        C    211
5    Y        C    213
6    Y        C    215
7    Y        D    219
8    Y        D    313

To make a crosstab for the ‘team’ and ‘position’ variables, use the following syntax.

How to perform One-Sample Wilcoxon Signed Rank Test in R? – Data Science Tutorials

library(dplyr)
library(tidyr)

Now we can produce the crosstab

df %>%
  group_by(team, position) %>%
  tally() %>%
  spread(team, n)
  position     X     Y
  <chr>    <int> <int>
1 A            2    NA
2 B            1    NA
3 C            1     2
4 D           NA     2

Here’s we can infer the values in the crosstab.

There is 2 player who has a position of ‘A’ and belongs to team ‘X’

There is 1 player who has a position of ‘B’ and belongs to team ‘X’

Arrange Data by Month in R with example – Data Science Tutorials

It’s worth noting that we may change the crosstab’s rows and columns by changing the value used in the spread() function.

library(dplyr)
library(tidyr)

Let’s produce a crosstab with ‘position’ along with columns.

Rejection Region in Hypothesis Testing – Data Science Tutorials

df %>%
  group_by(team, position) %>%
  tally() %>%
  spread(position, n)
team      A     B     C     D
  <chr> <int> <int> <int> <int>
1 X         2     1     1    NA
2 Y        NA    NA     2     2

Check your inbox or spam folder to confirm your subscription.

R Tags:dplyr

Post navigation

Previous Post: Augmented Dickey-Fuller Test in R
Next Post: Filtering for Unique Values in R- Using the dplyr

Related Posts

  • Survival Plot in R
    How to Perform a Log Rank Test in R R
  • How to create Sankey plot in R
    How to create a Sankey plot in R? R
  • How to Find Unmatched Records in R
    How to Find Unmatched Records in R R
  • How to copy files in R
    How to copy files in R R
  • Subsetting with multiple conditions in R
    Subsetting with multiple conditions in R R
  • Locate position of patterns in a character string in R R

Leave a Reply Cancel reply

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

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • 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.

  • Multiple regression model in R R
  • Descriptive statistics vs Inferential statistics
    Descriptive statistics vs Inferential statistics: Guide Statistics
  • Rounded corner bar plot in R
    How to make a rounded corner bar plot in R? R
  • In data science, what Is Open Innovation?
    In data science, what Is Open Innovation? R
  • How Cloud Computing Improves Workflows in Data Science
    How Cloud Computing Improves Workflows in Data Science Machine Learning
  • Top 7 Skills Required to Become a Data Scientist
    Top 7 Skills Required to Become a Data Scientist Machine Learning
  • Best GGPlot Themes
    Best GGPlot Themes You Should Know R
  • Ad Hoc Analysis
    What is Ad Hoc Analysis? Statistics

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme