library(tidyverse)
library(datasets)
iris datasetdata(iris)
head(iris, 3)
The object called ‘iris’ will be in the workspace. In this dataset, what is the mean of ‘Sepal.Width’ for the species setosa?
tapply(iris$Sepal.Width, iris$Species, mean)
setosa versicolor virginica
3.428 2.770 2.974
iris %>%
filter(Species == 'setosa') %>%
group_by(species = Species) %>%
summarise(avg_sepal_width = mean(Sepal.Width))
Return a list of the means of the variables ‘Sepal.Length’, ‘Sepal.Width’, ‘Petal.Length’, and ‘Petal.Width’?
lapply(iris[, -5], mean)
$Sepal.Length
[1] 5.843333
$Sepal.Width
[1] 3.057333
$Petal.Length
[1] 3.758
$Petal.Width
[1] 1.199333
Return a vector of the means of the variables ‘Sepal.Length’, ‘Sepal.Width’, ‘Petal.Length’, and ‘Petal.Width’?
sapply(iris[, -5], mean)
Sepal.Length Sepal.Width Petal.Length Petal.Width
5.843333 3.057333 3.758000 1.199333
sapply(iris[, 1:4], mean)
Sepal.Length Sepal.Width Petal.Length Petal.Width
5.843333 3.057333 3.758000 1.199333
Use apply() to find the means of the variables ‘Sepal.Length’, ‘Sepal.Width’, ‘Petal.Length’, and ‘Petal.Width’?
apply(iris[,1:4], 2, mean) #2 indicates columns
Sepal.Length Sepal.Width Petal.Length Petal.Width
5.843333 3.057333 3.758000 1.199333
NOTE
RowMeans, ColMeans, RowSums, ColSums are preferred functions since they have been optimized for the tasks.
colMeans(iris[1:4])
Sepal.Length Sepal.Width Petal.Length Petal.Width
5.843333 3.057333 3.758000 1.199333
data(mtcars)
head(mtcars,2)
Calculate the average weight (wt) by number of cylinders in the car (cyl)?
split(mtcars$wt, mtcars$cyl)
$`4`
[1] 2.320 3.190 3.150 2.200 1.615 1.835 2.465 1.935 2.140 1.513 2.780
$`6`
[1] 2.620 2.875 3.215 3.460 3.440 3.440 2.770
$`8`
[1] 3.440 3.570 4.070 3.730 3.780 5.250 5.424 5.345 3.520 3.435 3.840 3.845 3.170 3.570
sapply(split(mtcars$wt, mtcars$cyl), mean)
4 6 8
2.285727 3.117143 3.999214
tapply yields the same result above with the sapply-split combination
tapply(mtcars$wt, mtcars$cyl, mean)
4 6 8
2.285727 3.117143 3.999214
with(mtcars, tapply(wt, cyl, mean))
4 6 8
2.285727 3.117143 3.999214
mtcars %>%
group_by(cyl) %>%
summarise(avg_wt = mean(wt))