---
title: "Penguins EDA"
format:
html:
code-fold: true
---
## Penguin Size and Mass by Sex and Species
```{r}
library(palmerpenguins)
library(dplyr)
library(ggplot2)
df <- palmerpenguins::penguins
```
```{r}
df %>%
group_by(species, sex) %>%
summarise(
across(
where(is.numeric),
\(x) mean(x, na.rm = TRUE)
)
) %>%
knitr::kable()
```
## Penguin Size vs Mass by Species
```{r}
df %>%
ggplot(aes(x = bill_length_mm, y = body_mass_g, color = species)) +
geom_point() +
geom_smooth(method = "lm")
```
EDA in R
Modeling in Python fit a linear model to the penguins data. The eda-basic.qmd file runs an exploratory data analysis (EDA) of the same data in R. It groups the penguins by species and sex, summarizes the means of the numeric columns, and plots body mass against bill length.
Expand the section below to view the source file:
View eda-basic.qmd
I’ve also included the code from the eda-basic.qmd file in the sections below.
Load data
The code below loads palmerpenguins, dplyr, and ggplot2 and stores the penguins data as df:
library(palmerpenguins)
library(dplyr)
library(ggplot2)
df <- palmerpenguins::penguinsManipulate and summarize
The pipeline below returns a table with the mean of every numeric column by species and sex:
df %>%
dplyr::group_by(species, sex) %>%
dplyr::summarise(
dplyr::across(
dplyr::where(is.numeric),
\(x) mean(x, na.rm = TRUE)
)
) %>%
knitr::kable()| species | sex | bill_length_mm | bill_depth_mm | flipper_length_mm | body_mass_g | year |
|---|---|---|---|---|---|---|
| Adelie | female | 37.25753 | 17.62192 | 187.7945 | 3368.836 | 2008.055 |
| Adelie | male | 40.39041 | 19.07260 | 192.4110 | 4043.493 | 2008.055 |
| Adelie | NA | 37.84000 | 18.32000 | 185.6000 | 3540.000 | 2007.000 |
| Chinstrap | female | 46.57353 | 17.58824 | 191.7353 | 3527.206 | 2007.971 |
| Chinstrap | male | 51.09412 | 19.25294 | 199.9118 | 3938.971 | 2007.971 |
| Gentoo | female | 45.56379 | 14.23793 | 212.7069 | 4679.741 | 2008.069 |
| Gentoo | male | 49.47377 | 15.71803 | 221.5410 | 5484.836 | 2008.066 |
| Gentoo | NA | 45.62500 | 14.55000 | 215.7500 | 4587.500 | 2008.400 |
The table should have eight rows: female and male for each species, plus a row for the Adelie and Gentoo penguins with a missing (NA) sex.
Visualize
The scatter plot below shows body mass against bill length, with a linear fit line for each species:
df %>%
ggplot2::ggplot(
ggplot2::aes(
x = bill_length_mm,
y = body_mass_g,
color = species)) +
ggplot2::geom_point() +
ggplot2::geom_smooth(method = "lm")Each species should get its own colored line, and the Gentoo points should sit above the Adelie and Chinstrap points.
Recap
The
eda-basic.qmdfile loadspalmerpenguins::penguins, groups the data byspeciesandsex, and usesdplyr::across()withwhere(is.numeric)to compute the mean of every numeric column.The scatter plot of
body_mass_gagainstbill_length_mmadds ageom_smooth(method = "lm")line for each species. The model in Modeling in Python estimates this relationship with one shared slope: body mass increases with bill length, and species and sex shift the baseline.
Next, Lab 2: Project Architecture loads the penguins data into duckdb and turns the model into a vetiver API.
