Front
Back
How do you compute grouped summary statistics quickly?
Use dplyr::group_by() + summarise().
Code:
library(dplyr)
mtcars %>% group_by(cyl) %>% summarise(across(mpg:hp, list(mean=mean, sd=sd)))
How do you make a scatter plot with ggplot2?
Use geom_point().
Code:
library(ggplot2)
ggplot(mtcars, aes(wt, mpg)) + geom_point() + labs(x=’Weight’, y=’MPG’)
How do you plot a histogram and a boxplot?
Use geom_histogram() for distributions; geom_boxplot() for spread/outliers.
Code:
library(ggplot2)
ggplot(mtcars, aes(mpg)) + geom_histogram(bins = 20)
ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot()
How do you facet plots by a category?
Use facet_wrap(~ var).
Code:
library(ggplot2)
ggplot(iris, aes(Sepal.Length, Petal.Length)) + geom_point() + facet_wrap(~ Species)
How do you compute and test correlations?
Use cor() for coefficient, cor.test() for inference.
Code:
cor(mtcars$mpg, mtcars$wt, method=’pearson’)
cor.test(mtcars$mpg, mtcars$wt)