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