week 3 exercises

Author

Kate

Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see https://quarto.org.

Running Code

When you click the Render button a document will be generated that includes both content and the output of embedded code. You can embed code like this:

1 + 1
[1] 2

You can add options to executable code like this

[1] 4

The echo: false option disables the printing of code (only output is displayed).

1/200*30
[1] 0.15
sqrt(24)
[1] 4.898979
7*7
[1] 49
a <- 1+2
b <- 3+4
c <- "objects can be of different types"
c
[1] "objects can be of different types"
(d <- "another new string")
[1] "another new string"
a+b
[1] 10
this_is_a_really_long_name <- 2.5
this_is_too <- 3.5
r_rocks <- 2^3
colour <- "blue"
seq(1,20)
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
x <- "hello world"
(y <- seq(1,10, length.out = 5))
[1]  1.00  3.25  5.50  7.75 10.00
library(palmerpenguins)

Attaching package: 'palmerpenguins'
The following objects are masked from 'package:datasets':

    penguins, penguins_raw
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.2     ✔ tibble    3.3.0
✔ lubridate 1.9.4     ✔ tidyr     1.3.1
✔ purrr     1.1.0     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
penguins |> 
  filter(species == "adelie") |> 
  arrange(body_mass_g)
# A tibble: 0 × 8
# ℹ 8 variables: species <fct>, island <fct>, bill_length_mm <dbl>,
#   bill_depth_mm <dbl>, flipper_length_mm <int>, body_mass_g <int>, sex <fct>,
#   year <int>
# which penguins weigh more than 5000g?
penguins %>% 
  filter(body_mass_g > 5000)
# A tibble: 61 × 8
   species island bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
   <fct>   <fct>           <dbl>         <dbl>             <int>       <int>
 1 Gentoo  Biscoe           50            16.3               230        5700
 2 Gentoo  Biscoe           50            15.2               218        5700
 3 Gentoo  Biscoe           47.6          14.5               215        5400
 4 Gentoo  Biscoe           46.7          15.3               219        5200
 5 Gentoo  Biscoe           46.8          15.4               215        5150
 6 Gentoo  Biscoe           49            16.1               216        5550
 7 Gentoo  Biscoe           48.4          14.6               213        5850
 8 Gentoo  Biscoe           49.3          15.7               217        5850
 9 Gentoo  Biscoe           49.2          15.2               221        6300
10 Gentoo  Biscoe           48.7          15.1               222        5350
# ℹ 51 more rows
# ℹ 2 more variables: sex <fct>, year <int>
# shows the relationship between a penguins flipper length and its body mass
## do heavier penguins tend to have longer flippers?
penguins |> 
  ggplot(aes(x = flipper_length_mm,
             y= body_mass_g,
             colour = species)) +
  geom_point()
Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).

#
penguins |> 
  ggplot(aes(x= flipper_length_mm,
             y= body_mass_g)) +
  geom_point() +
  labs(
    tittle = "body mass by flipper length",
    x= "flipper length (mm)",
    y= "body mass (g)"
  )
Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).

# compares average body mass across different species
## which penguiin species is the heaviest on average?
penguins |> 
  ggplot(aes(x= species,
             y= body_mass_g)) +
  geom_point() +
  labs(
    tittle = "body mass by species",
    x = "species",
    y = "body mass (g)"
  )
Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).

# displays the distribution, spread, and outliers of body mass for each species
## which species has the most variation in body mass?
penguins |> 
  group_by(species) |> 
  summarise(mean_body_mass_g =
              mean(body_mass_g, na.rm = TRUE)) |> 
  ungroup() |> 
  ggplot(aes(x= species,
             y= mean_body_mass_g)) +
  geom_col() +
  labs(
    title = "body mass by species",
    x = "species",
    y= "mean body mass (g)"
  )

# tracks population trends over time for each species
## how has the number of adelie penguins changed over the years?
penguins |> 
  count(year, species) |> 
  ggplot(aes(x = year,
             y = n,
             colour = species)) +
  geom_line() +
  labs(
    title = "penguin count per year",
    suntitle = "and species",
    x= "year",
    y= "number of penguin sightings"
  )

# shows the frequency distribution of bill depth each year, seperated by species.
## did the average bill depth of gentoo penguins change over time?
penguins |> 
  ggplot(aes(x = bill_depth_mm,
             colour = species)) +
  geom_histogram(binwidth = 1) +
  labs(
    title = "bill depth histogram by species",
    x = "bill depth",
    y = "count"
  ) +
  facet_wrap(~species)
Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_bin()`).

# compares the distribution of bill depth between species
## which species has the deepest bills on average?
penguins |> 
  ggplot(aes(x = species,
             y = bill_length_mm,
             fill = species)) +
  geom_boxplot() +
  labs(
    title = "bill depth by species",
    x = "species",
    y= "bill depth"
  )
Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_boxplot()`).

# shows how bill deoth varies across species and islands, highlighting both species differences and location effects.
## do penguins of the same species have different bill depths depending on the island they live on?
penguins |> 
  ggplot(aes(x = species,
             y= bill_depth_mm,
             fill = island)) +
  geom_boxplot() +
  labs(
    title = "bill depth by species and island",
    x = "species",
    y = "bill depth",
    fill = "island"
  )
Warning: Removed 2 rows containing non-finite outside the scale range
(`stat_boxplot()`).