How to create summary table in R
How to create summary table in R, Using the describe() and describeBy() methods from the psych library is the most straightforward method for creating summary tables in R.
How to create summary table in R
To create summary tables in R using the `psych` library, you can follow these steps:
1. Basic Summary Table:
library(psych) df <- data.frame(team=c('A', 'A', 'B', 'B', 'C', 'C', 'C'), points=c(15, 22, 29, 41, 30, 11, 19), rebounds=c(17, 28, 36, 36, 7, 19, 13), steals=c(1, 1, 2, 3, 5, 7, 5)) df
Let’s create summary table
ggplot2 colors in R-Default colors complete guide » finnstats
describe(df) vars n mean sd median trimmed mad min max range skew kurtosis se team* 1 7 2.14 0.90 2 2.14 1.48 1 3 2 -0.22 -1.90 0.34 points 2 7 23.86 10.24 22 23.86 10.38 11 41 30 0.33 -1.41 3.87 rebounds 3 7 22.29 11.31 19 22.29 13.34 7 36 29 0.08 -1.82 4.27 steals 4 7 3.43 2.30 3 3.43 2.97 1 7 6 0.25 -1.73 0.87
In the first example, we load the `psych` library and then use the `describe()` function to create a summary table for each variable in the data frame. We can view the output using the `view()` function.
2. Summary Table, Grouped by Specific Variable:
The code below demonstrates how to arrange the data frame by the “team” variable to produce a summary table using the describeBy() function:
library(psych)
Let’s create summary table, grouped by ‘team’ variable
describeBy(df, group=df$team, fast=TRUE) Descriptive statistics by group group: A vars n mean sd min max range se team* 1 2 1.0 0.00 1 1 0 0.0 points 2 2 18.5 4.95 15 22 7 3.5 rebounds 3 2 22.5 7.78 17 28 11 5.5 steals 4 2 1.0 0.00 1 1 0 0.0 ---------------------------------------------------------------------------------- group: B vars n mean sd min max range se team* 1 2 2.0 0.00 2 2 0 0.0 points 2 2 35.0 8.49 29 41 12 6.0 rebounds 3 2 36.0 0.00 36 36 0 0.0 steals 4 2 2.5 0.71 2 3 1 0.5 ---------------------------------------------------------------------------------- group: C vars n mean sd min max range se team* 1 3 3.00 0.00 3 3 0 0.00 points 2 3 20.00 9.54 11 30 19 5.51 rebounds 3 3 13.00 6.00 7 19 12 3.46 steals 4 3 5.67 1.15 5 7 2 0.67
In the second example, we load the `psych` library and then use the `describeBy()` function to create a summary table for each variable in the data frame, grouped by a specific variable (in this case, ‘team’).
We set the `fast=TRUE` argument to only calculate the most common summary statistics. After creating the summary table, we can view it using the `view()` function.
The output will show descriptive statistics for each group (in this case, A, B, C).
How to copy files in R » Data Science Tutorials