|>
penguins filter(
!is.na(sex) # Exclude those where sex is missing
|>
) count(species, sex)
RAdelaide 2025
July 8, 2025
tidyr
tidyr
tidyr
pivot_longer()
and pivot_wider()
separate()
and unite()
library(tidyr)
to the beginning of the scriptpenguins
has each measurement in long formpivot_wider()
can be helpfulpivot_longer()
separate()
into
argumentSetting The Wrong Number of Columns
?separate
sep = "[^[:alnum:]]+"
is a regular expressionvalues_fill = 0
to get rid of missing values in the output# A tibble: 9 × 5
island year Adelie Chinstrap Gentoo
<fct> <int> <int> <int> <int>
1 Biscoe 2007 10 0 34
2 Biscoe 2008 18 0 46
3 Biscoe 2009 16 0 44
4 Dream 2007 20 26 0
5 Dream 2008 16 18 0
6 Dream 2009 20 24 0
7 Torgersen 2007 20 0 0
8 Torgersen 2008 16 0 0
9 Torgersen 2009 16 0 0
total
which adds the values for all species columnsisland:year
bill_length_mm
# A tibble: 5 × 5
species island `2007` `2008` `2009`
<fct> <fct> <dbl> <dbl> <dbl>
1 Adelie Torgersen 38.8 38.8 39.3
2 Adelie Biscoe 38.3 38.7 39.7
3 Adelie Dream 39.1 38.2 38.2
4 Gentoo Biscoe 47.0 46.9 48.5
5 Chinstrap Dream 48.7 48.7 49.1
overall_mean
which averages the values for all years