Polar bears

In this activity we’ll explore a small data set on polar bears from Skandinavisk Dyrepark. The data includes weights of 8 polar bears across years - 4 males and 4 females (2 cubs born in autumn/winter 2021). One male and one female only have one weight. And the cubs have a limited number of weights due to their age and have not yet reach adult weight.

We begin by loading some packages

library("readxl")
library("dplyr")

Attaching package: 'dplyr'
The following objects are masked from 'package:stats':

    filter, lag
The following objects are masked from 'package:base':

    intersect, setdiff, setequal, union
library("stringr")
library("janitor")

Attaching package: 'janitor'
The following objects are masked from 'package:stats':

    chisq.test, fisher.test

Next we will load the data into R

polar_bear <- read_xlsx("data/polar-bears/weight-polar-bear.xlsx")

Next we clean the names of the variables

polar_bear <- polar_bear |>
  janitor::clean_names()

Now, we need to clean the data a little, removing some blank or incomplete rows at the end of the data frame

polar_bear <- polar_bear |>
  filter(!is.na(preferred_id))

We need to create a variable sex, as this is bound up as part of the preferred_id variable.

polar_bear <- polar_bear |>
  mutate(sex = str_sub(preferred_id, start = 1, end = 1))

It would nice if we could have the sex variable have labels female and male

polar_bear <- polar_bear |>
  mutate(
    sex = case_when(
      sex == "F" ~ "female",
      sex == "M" ~ "male"
    )
  )

Finally, we can rename the value variable to be weight

polar_bear <- polar_bear |>
  rename(weight = value)
NoteQuestion

Look at polar_bear; the note column is empty. Let’s remove it. How would you do using the dplyr verbs you know?

polar_bear <- polar_bear |>
  select(-note)
NoteQuestion

Summarise polar_bear to show the maximum weight of any polar bear. What is this maximum weight?

polar_bear |>
  summarise(max_weight = max(weight, na.rm = TRUE))
# A tibble: 1 × 1
  max_weight
       <dbl>
1        691

The maximum weight is 691

NoteQuestion

Summarise polar_bear to show the minimum weight of polar bear F3. What is the minimum weight of this polar bear?

polar_bear |>
  filter(preferred_id == "F3") |>
  summarise(min_weight = min(weight, na.rm = TRUE))
# A tibble: 1 × 1
  min_weight
       <dbl>
1        7.4

The minimum weight is 7.4

NoteQuestion

What is the mean and the maximum weight for the male and female polar bears, separately?

polar_bear |>
  group_by(sex) |>
  summarise(
    max_weight = max(weight, na.rm = TRUE),
    mean_weight = mean(weight, na.rm = TRUE)
  )
Warning: There was 1 warning in `summarise()`.
ℹ In argument: `max_weight = max(weight, na.rm = TRUE)`.
ℹ In group 3: `sex = NA`.
Caused by warning in `max()`:
! no non-missing arguments to max; returning -Inf
# A tibble: 3 × 3
  sex    max_weight mean_weight
  <chr>       <dbl>       <dbl>
1 female        371        195.
2 male          691        533.
3 <NA>         -Inf        NaN 
NoteQuestion

How many weight observations do we have for each polar bear?

polar_bear |>
  group_by(preferred_id) |>
  summarise(n = n())
# A tibble: 9 × 2
  preferred_id     n
  <chr>        <int>
1 F1              25
2 F2               6
3 F3               6
4 F4               1
5 M1               1
6 M2              57
7 M3              50
8 M4               6
9 <NA>             3