Every analysis should start with description: what do the variables look like, one at a time and group by group? Reviewing descriptives first catches broken data before it can break your conclusions, and the values you compute here (means, standard deviations, counts) are exactly what goes in Table 1 of a paper.
One variable at a time
For a numeric variable you usually report a measure of centre and a measure of spread. summary() gives the five-number summary plus the mean in one call.
mpg <- mtcars$mpg
mean(mpg) # centre: mean
median(mpg) # centre: median
sd(mpg) # spread: standard deviation
summary(mpg) # min, quartiles, median, mean, maxOutput
[1] 20.09062
[1] 19.2
[1] 6.026948
Min. 1st Qu. Median Mean 3rd Qu. Max.
10.40 15.43 19.20 20.09 22.80 33.90When a variable is skewed or has outliers, report the median and interquartile range instead of the mean and SD; comparing mean() and median() is the quickest skew check there is.
Counting categories
For categorical variables you want frequencies. table() counts; prop.table() turns counts into proportions.
counts <- table(mtcars$cyl) # cars by cylinder count
counts
prop.table(counts) # as proportionsOutput
4 6 8
11 7 14
4 6 8
0.34375 0.21875 0.43750Summaries by group
Most research questions compare groups, so you need descriptives per group. aggregate() applies a function to a numeric variable within each level of a grouping variable, using R's formula notation: outcome ~ group, read "outcome by group".
# Mean and SD of fuel efficiency by cylinder count
aggregate(mpg ~ cyl, data = mtcars, FUN = mean)
aggregate(mpg ~ cyl, data = mtcars, FUN = sd)Output
cyl mpg
1 4 26.66364
2 6 19.74286
3 8 15.10000
cyl mpg
1 4 4.509828
2 6 1.453567
3 8 2.560048That formula notation is worth memorising now: the tests in the next lessons (t.test, aov, lm) all use the same outcome ~ group pattern.
What goes in the write-up
- Numeric variables: mean and SD (or median and IQR if skewed), plus n
- Categorical variables: counts and percentages per level
- Grouped designs: descriptives per group, not just overall
These are the numbers a reader needs to judge every test that follows. Speaking of which: the t-test is next.
