Body mass of penguins in 2009
Exercise Type: Implementation
Instructions
Calculate the median body mass of penguin species that were observed at the Palmer station in 2009.
-
Download this file: penguins.csv
-
Read it into R with
read.csv()
! Check its structure and familiarize yourself with the columns. Every row represents one observed penguin. -
Subset the data to the the year 2009!
-
List the species that were observed this year (every species name occurs once)!
-
Select one species, e.g. the
Gentoo
, and subset the 2009 data to the corresponding part. Calculate the median body mass for this species. Hint: missing values can be omitted if you usemedian(x , na.rm=TRUE)
where x is the input vector to the function. -
Repeat step 5 with a
for()
loop, calculate the median mass of every species. -
Plot a boxplot with adapting this code to your variable names (
yearDat
is thedata.frame
in my code):
boxplot(body_mass_g ~ species, data=yearDat)
Check your results with this. The thick black horizontal lines should indicate the medians that you calculated earlier.
Expected outputs
- A named numeric vector:
names
: names of species values
: the median body mass of species
- A boxplot