Skip to main content

EDA in R

Published

2026-11-06

The eda-basic.qmd file groups the penguins by species and sex, summarizes the means of the numeric columns, and plots bill length against body mass.

Expand the section below to view the source file:

View eda-basic.qmd


---
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")
```

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::penguins

Manipulate 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

Visualize

The scatter plot below shows bill length against body mass, 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")

Recap

  1. The eda-basic.qmd file loads palmerpenguins::penguins, groups the data by species and sex, and uses dplyr::across() with where(is.numeric) to compute the mean of every numeric column.

  2. The scatter plot of bill_length_mm against body_mass_g adds a geom_smooth(method = "lm") line for each species. That linear relationship is what the model in Modeling in Python estimates: body mass increases with bill length, and species shifts the baseline.

Next, Lab 2: Project Architecture loads the penguins data into duckdb and turns the model into a vetiver API.