Foundations of Functional Programming with purrr
Auriel Fournier
Instructor
lo[["data"]] <- data.frame(bird = c("robin", "sparrow", "jay"),
weight = c(76, 14, 100),
wing_length = c(100, 35, 130))
lo[["model"]] <- lm(weight ~ wing_length,
data = lo[["data"]])
lo[["plot"]] <- ggplot(
data = lo[["model"]],
aes(x = weight, y = wing_length)) +
geom_point()
data frames
mtcars[1, "wt"]
mtcars$wt
2.62
[1] 2.620 2.875 2.320 3.215 3.440
[6] 3.460 3.570 3.190 3.150 3.440
[11] 3.440 4.070 3.730 3.780 5.250
[16] 5.424 5.345 2.200 1.615 1.835
[21] 2.465 3.520 3.435 3.840 3.845
[26] 1.935 2.140 1.513 3.170 2.770
[31] 3.570 2.780
Lists
lo[[2]]
Call:
lm(formula = weight ~ wing_length,
data = lo[["data"]])
Coefficients:
(Intercept) wing_length
-17.3216 0.9131
lo[["model"]]
Call:
lm(formula = weight ~ wing_length,
data = lo[["data"]])
Coefficients:
(Intercept) wing_length
-17.3216 0.9131
# Create a data frame to place the results in
df_rows <- data.frame(names = names(survey_data),
rows = NA)
# Loop over survey_data to determine how many
# rows are in each element
for(i in seq_along(survey_data)){
df_rows[i,'rows'] <- nrow(survey_data[[i]])
}
# Print out survey_rows
df_rows
names rows
1 LakeErieS 14
2 LakeErieN 14
3 LakeErieW 14
4 LakeErieE 15
# Get a summary of survey_data
summary(survey_data)
Length Class Mode
LakeErieS 2 data.frame list
LakeErieN 2 data.frame list
LakeErieW 2 data.frame list
LakeErieE 2 data.frame list
# Determine row num in survey_data
map(survey_data, ~nrow(.x))
$`LakeErieS`
[1] 14
$`LakeErieN`
[1] 14
$`LakeErieW`
[1] 14
$`LakeErieE`
[1] 15
Foundations of Functional Programming with purrr