Variable Min Median Mean Sd Max
1 mpg 10 19 20 6 34
2 disp 71 196 231 124 472
3 hp 52 123 147 69 335
4 drat 2.8 3.7 3.6 0.53 4.9
5 wt 1.5 3.3 3.2 0.98 5.4
6 qsec 14 18 18 1.8 23
2026-11-22
Week TWO
Note that R allows you to assign names to rows of a dataframe just as you can assign names to columns of a dataframe. We saw an example of that with the mtcars data, which appeared to have an extra column because the car makes and models were assigned as rownames.
mean(vectorname) in R if vector is numericcolMeans(df) or sapply(df,mean)colMeans(mtcars[,c(1,2:4)])colMeans(df[,which(sapply(df,is.numeric))])df<-mtcars colMeans(df[which(df$hp>100),which(sapply(df,is.numeric))]) median(vectorname) in R if vector is numericsd(vectorname)str(df) in R to get the following
summary(df) in R to get an entry for each column, containing
Variable Min Median Mean Sd Max
1 mpg 10 19 20 6 34
2 disp 71 196 231 124 472
3 hp 52 123 147 69 335
4 drat 2.8 3.7 3.6 0.53 4.9
5 wt 1.5 3.3 3.2 0.98 5.4
6 qsec 14 18 18 1.8 23
First, get some categorical data …
Classes 'tbl_df', 'tbl' and 'data.frame': 89 obs. of 2 variables:
$ group : Factor w/ 2 levels "control","treatment": 2 2 2 2 2 2 2 2 2 2 ...
$ pain_free: Factor w/ 2 levels "no","yes": 2 2 2 2 2 2 2 2 2 2 ...
grade
loan_purpose A B C D E F G Sum
0 0 0 0 0 0 0 0 0
car 0 0 1 1 0 0 0 0 2
credit_card 0 6 4 1 1 1 0 0 13
debt_consolidation 0 2 9 4 7 1 0 0 23
home_improvement 0 1 4 0 0 0 0 0 5
house 0 0 1 0 0 0 0 0 1
major_purchase 0 0 0 0 0 0 0 0 0
medical 0 0 0 0 0 0 0 0 0
moving 0 0 0 0 0 0 0 0 0
other 0 4 0 0 0 0 0 0 4
renewable_energy 0 1 0 0 0 0 0 0 1
small_business 0 1 0 0 0 0 0 0 1
vacation 0 0 0 0 0 0 0 0 0
wedding 0 0 0 0 0 0 0 0 0
Sum 0 15 19 6 8 2 0 0 50
mtcars#. install.packages("pacman")
pacman::p_load(tidyverse)
ggplot(mtcars, aes(x = hp, y = mpg, size = cyl, color = factor(cyl))) +
geom_point(alpha = 0.7) +
scale_size_continuous(range = c(2, 10)) +
scale_color_manual(values = c("#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f", "#bcbd22", "#17becf")) +
labs(x = "Horsepower", y = "MPG", size = "Number of Cylinders", color = "Number of Cylinders", title = "MPG vs. Horsepower (Bubble Size: Number of Cylinders)") +
theme_minimal()ggplot, the main function in that set of packagesIn these parts, a man’s life may depend on a mere scrap of information.
— Clint Eastwood, in A Fistful of Dollars (1964)
END
This slideshow was produced using quarto
Fonts are Roboto Condensed Bold, JetBrains Mono Nerd Font, and STIX2