6B: Using Data Manipulation to Understand Mammalian Sleep

From Freepik
library(tidyverse)

glimpse(msleep)
Rows: 83
Columns: 11
$ name         <chr> "Cheetah", "Owl monkey", "Mountain beaver", "Greater shor…
$ genus        <chr> "Acinonyx", "Aotus", "Aplodontia", "Blarina", "Bos", "Bra…
$ vore         <chr> "carni", "omni", "herbi", "omni", "herbi", "herbi", "carn…
$ order        <chr> "Carnivora", "Primates", "Rodentia", "Soricomorpha", "Art…
$ conservation <chr> "lc", NA, "nt", "lc", "domesticated", NA, "vu", NA, "dome…
$ sleep_total  <dbl> 12.1, 17.0, 14.4, 14.9, 4.0, 14.4, 8.7, 7.0, 10.1, 3.0, 5…
$ sleep_rem    <dbl> NA, 1.8, 2.4, 2.3, 0.7, 2.2, 1.4, NA, 2.9, NA, 0.6, 0.8, …
$ sleep_cycle  <dbl> NA, NA, NA, 0.1333333, 0.6666667, 0.7666667, 0.3833333, N…
$ awake        <dbl> 11.9, 7.0, 9.6, 9.1, 20.0, 9.6, 15.3, 17.0, 13.9, 21.0, 1…
$ brainwt      <dbl> NA, 0.01550, NA, 0.00029, 0.42300, NA, NA, NA, 0.07000, 0…
$ bodywt       <dbl> 50.000, 0.480, 1.350, 0.019, 600.000, 3.850, 20.490, 0.04…

From Science Notes

From Reclaimed Earth

First Part

💻 Hands-On

Subset msleep to only herbivores. How many herbivores are there?

There are 32 herbivores.

msleep |>
  filter(vore == 'herbi') |>
  count()
# A tibble: 1 × 1
      n
  <int>
1    32

💻 Hands-On

Subset msleep to only animals who are awake for at least 12 hours of the day. How many such animals are there?

There are 52 animals who are awake for at least 12 hours of the day.

msleep |>
  filter(awake >= 12) |>
  count()
# A tibble: 1 × 1
      n
  <int>
1    52

💻 Hands-On

Subset msleep to only herbivores who are awake for at least 12 hours of the days. How many such animals are there?

There are 18 herbivores who are awake for at least 12 hours of the days.

msleep |>
  filter(vore == 'herbi', awake >= 12) |>
  count()
# A tibble: 1 × 1
      n
  <int>
1    18

💻 Hands-On

Subset msleep to include only herbivores and insectivores. You may find the operator %in% useful.

There are two ways to do this: the or | operator and the %in% operator.

# 'or' operator
msleep |>
  filter(vore == 'herbi' | vore == 'insecti')
# A tibble: 37 × 11
   name   genus vore  order conservation sleep_total sleep_rem sleep_cycle awake
   <chr>  <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
 1 Mount… Aplo… herbi Rode… nt                  14.4       2.4      NA       9.6
 2 Cow    Bos   herbi Arti… domesticated         4         0.7       0.667  20  
 3 Three… Brad… herbi Pilo… <NA>                14.4       2.2       0.767   9.6
 4 Roe d… Capr… herbi Arti… lc                   3        NA        NA      21  
 5 Goat   Capri herbi Arti… lc                   5.3       0.6      NA      18.7
 6 Guine… Cavis herbi Rode… domesticated         9.4       0.8       0.217  14.6
 7 Chinc… Chin… herbi Rode… domesticated        12.5       1.5       0.117  11.5
 8 Tree … Dend… herbi Hyra… lc                   5.3       0.5      NA      18.7
 9 Asian… Elep… herbi Prob… en                   3.9      NA        NA      20.1
10 Big b… Epte… inse… Chir… lc                  19.7       3.9       0.117   4.3
# ℹ 27 more rows
# ℹ 2 more variables: brainwt <dbl>, bodywt <dbl>
# %in%
msleep |>
  filter(vore %in% c('herbi', 'insecti'))
# A tibble: 37 × 11
   name   genus vore  order conservation sleep_total sleep_rem sleep_cycle awake
   <chr>  <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
 1 Mount… Aplo… herbi Rode… nt                  14.4       2.4      NA       9.6
 2 Cow    Bos   herbi Arti… domesticated         4         0.7       0.667  20  
 3 Three… Brad… herbi Pilo… <NA>                14.4       2.2       0.767   9.6
 4 Roe d… Capr… herbi Arti… lc                   3        NA        NA      21  
 5 Goat   Capri herbi Arti… lc                   5.3       0.6      NA      18.7
 6 Guine… Cavis herbi Rode… domesticated         9.4       0.8       0.217  14.6
 7 Chinc… Chin… herbi Rode… domesticated        12.5       1.5       0.117  11.5
 8 Tree … Dend… herbi Hyra… lc                   5.3       0.5      NA      18.7
 9 Asian… Elep… herbi Prob… en                   3.9      NA        NA      20.1
10 Big b… Epte… inse… Chir… lc                  19.7       3.9       0.117   4.3
# ℹ 27 more rows
# ℹ 2 more variables: brainwt <dbl>, bodywt <dbl>

💻 Hands-On

Subset msleep to include only herbivores and carnivores who sleep at least 12 hours a day.

# 'or' operator
msleep |>
  filter(vore == 'herbi' | vore == 'carni', sleep_total >= 12)
# A tibble: 22 × 11
   name   genus vore  order conservation sleep_total sleep_rem sleep_cycle awake
   <chr>  <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
 1 Cheet… Acin… carni Carn… lc                  12.1      NA        NA      11.9
 2 Mount… Aplo… herbi Rode… nt                  14.4       2.4      NA       9.6
 3 Three… Brad… herbi Pilo… <NA>                14.4       2.2       0.767   9.6
 4 Chinc… Chin… herbi Rode… domesticated        12.5       1.5       0.117  11.5
 5 Long-… Dasy… carni Cing… lc                  17.4       3.1       0.383   6.6
 6 Weste… Euta… herbi Rode… <NA>                14.9      NA        NA       9.1
 7 Domes… Felis carni Carn… domesticated        12.5       3.2       0.417  11.5
 8 Thick… Lutr… carni Dide… lc                  19.4       6.6      NA       4.6
 9 Mongo… Meri… herbi Rode… lc                  14.2       1.9      NA       9.8
10 Golde… Meso… herbi Rode… en                  14.3       3.1       0.2     9.7
# ℹ 12 more rows
# ℹ 2 more variables: brainwt <dbl>, bodywt <dbl>
# %in%
msleep |>
  filter(vore %in% c('herbi', 'carni'), sleep_total >= 12)
# A tibble: 22 × 11
   name   genus vore  order conservation sleep_total sleep_rem sleep_cycle awake
   <chr>  <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
 1 Cheet… Acin… carni Carn… lc                  12.1      NA        NA      11.9
 2 Mount… Aplo… herbi Rode… nt                  14.4       2.4      NA       9.6
 3 Three… Brad… herbi Pilo… <NA>                14.4       2.2       0.767   9.6
 4 Chinc… Chin… herbi Rode… domesticated        12.5       1.5       0.117  11.5
 5 Long-… Dasy… carni Cing… lc                  17.4       3.1       0.383   6.6
 6 Weste… Euta… herbi Rode… <NA>                14.9      NA        NA       9.1
 7 Domes… Felis carni Carn… domesticated        12.5       3.2       0.417  11.5
 8 Thick… Lutr… carni Dide… lc                  19.4       6.6      NA       4.6
 9 Mongo… Meri… herbi Rode… lc                  14.2       1.9      NA       9.8
10 Golde… Meso… herbi Rode… en                  14.3       3.1       0.2     9.7
# ℹ 12 more rows
# ℹ 2 more variables: brainwt <dbl>, bodywt <dbl>

💻 Hands-On

Subset msleep to only carnivores who weigh more than 50 kg.

msleep |>
  filter(vore == 'carni', bodywt > 50)
# A tibble: 8 × 11
  name    genus vore  order conservation sleep_total sleep_rem sleep_cycle awake
  <chr>   <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
1 Pilot … Glob… carni Ceta… cd                   2.7       0.1          NA  21.4
2 Gray s… Hali… carni Carn… lc                   6.2       1.5          NA  17.8
3 Tiger   Pant… carni Carn… en                  15.8      NA            NA   8.2
4 Jaguar  Pant… carni Carn… nt                  10.4      NA            NA  13.6
5 Lion    Pant… carni Carn… vu                  13.5      NA            NA  10.5
6 Caspia… Phoca carni Carn… vu                   3.5       0.4          NA  20.5
7 Common… Phoc… carni Ceta… vu                   5.6      NA            NA  18.4
8 Bottle… Turs… carni Ceta… <NA>                 5.2      NA            NA  18.8
# ℹ 2 more variables: brainwt <dbl>, bodywt <dbl>

💻 Hands-On

Subset msleep to keep only the columns name, awake, sleep_total, sleep_rem, and sleep_cycle.

msleep |>
  select(name, awake, sleep_total, sleep_rem, sleep_cycle)
# A tibble: 83 × 5
   name                       awake sleep_total sleep_rem sleep_cycle
   <chr>                      <dbl>       <dbl>     <dbl>       <dbl>
 1 Cheetah                     11.9        12.1      NA        NA    
 2 Owl monkey                   7          17         1.8      NA    
 3 Mountain beaver              9.6        14.4       2.4      NA    
 4 Greater short-tailed shrew   9.1        14.9       2.3       0.133
 5 Cow                         20           4         0.7       0.667
 6 Three-toed sloth             9.6        14.4       2.2       0.767
 7 Northern fur seal           15.3         8.7       1.4       0.383
 8 Vesper mouse                17           7        NA        NA    
 9 Dog                         13.9        10.1       2.9       0.333
10 Roe deer                    21           3        NA        NA    
# ℹ 73 more rows

💻 Hands-On

Subset msleep to keep all columns except genus and order.

msleep |>
  select(-genus, -order)
# A tibble: 83 × 9
   name      vore  conservation sleep_total sleep_rem sleep_cycle awake  brainwt
   <chr>     <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>    <dbl>
 1 Cheetah   carni lc                  12.1      NA        NA      11.9 NA      
 2 Owl monk… omni  <NA>                17         1.8      NA       7    0.0155 
 3 Mountain… herbi nt                  14.4       2.4      NA       9.6 NA      
 4 Greater … omni  lc                  14.9       2.3       0.133   9.1  0.00029
 5 Cow       herbi domesticated         4         0.7       0.667  20    0.423  
 6 Three-to… herbi <NA>                14.4       2.2       0.767   9.6 NA      
 7 Northern… carni vu                   8.7       1.4       0.383  15.3 NA      
 8 Vesper m… <NA>  <NA>                 7        NA        NA      17   NA      
 9 Dog       carni domesticated        10.1       2.9       0.333  13.9  0.07   
10 Roe deer  herbi lc                   3        NA        NA      21    0.0982 
# ℹ 73 more rows
# ℹ 1 more variable: bodywt <dbl>

💻 Hands-On

Move vore to the front of msleep. You may find the everything() function useful.

The everything() function automatically includes all the variables that have not been selected.

msleep |>
  select(vore, everything())
# A tibble: 83 × 11
   vore  name   genus order conservation sleep_total sleep_rem sleep_cycle awake
   <chr> <chr>  <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
 1 carni Cheet… Acin… Carn… lc                  12.1      NA        NA      11.9
 2 omni  Owl m… Aotus Prim… <NA>                17         1.8      NA       7  
 3 herbi Mount… Aplo… Rode… nt                  14.4       2.4      NA       9.6
 4 omni  Great… Blar… Sori… lc                  14.9       2.3       0.133   9.1
 5 herbi Cow    Bos   Arti… domesticated         4         0.7       0.667  20  
 6 herbi Three… Brad… Pilo… <NA>                14.4       2.2       0.767   9.6
 7 carni North… Call… Carn… vu                   8.7       1.4       0.383  15.3
 8 <NA>  Vespe… Calo… Rode… <NA>                 7        NA        NA      17  
 9 carni Dog    Canis Carn… domesticated        10.1       2.9       0.333  13.9
10 herbi Roe d… Capr… Arti… lc                   3        NA        NA      21  
# ℹ 73 more rows
# ℹ 2 more variables: brainwt <dbl>, bodywt <dbl>

💻 Hands-On

Move bodywt and brainwt to the front of msleep.

msleep |>
  select(bodywt, brainwt, everything())
# A tibble: 83 × 11
    bodywt  brainwt name    genus vore  order conservation sleep_total sleep_rem
     <dbl>    <dbl> <chr>   <chr> <chr> <chr> <chr>              <dbl>     <dbl>
 1  50     NA       Cheetah Acin… carni Carn… lc                  12.1      NA  
 2   0.48   0.0155  Owl mo… Aotus omni  Prim… <NA>                17         1.8
 3   1.35  NA       Mounta… Aplo… herbi Rode… nt                  14.4       2.4
 4   0.019  0.00029 Greate… Blar… omni  Sori… lc                  14.9       2.3
 5 600      0.423   Cow     Bos   herbi Arti… domesticated         4         0.7
 6   3.85  NA       Three-… Brad… herbi Pilo… <NA>                14.4       2.2
 7  20.5   NA       Northe… Call… carni Carn… vu                   8.7       1.4
 8   0.045 NA       Vesper… Calo… <NA>  Rode… <NA>                 7        NA  
 9  14      0.07    Dog     Canis carni Carn… domesticated        10.1       2.9
10  14.8    0.0982  Roe de… Capr… herbi Arti… lc                   3        NA  
# ℹ 73 more rows
# ℹ 2 more variables: sleep_cycle <dbl>, awake <dbl>

💻 Hands-On

What does the following code do? You may find running the code line by line helpful.

msleep |>
  mutate(class = "Mammalia") |>
  select(class)

The code creates a new column named class with only one value Mammalia.

💻 Hands-On

Create the following columns:

  • bodywt_g which contains the body weight but in grams instead of kg

  • log_bodywt that gives the natural logarithm of the body weight

  • percent_day_awake that gives the percentage of the day that each species spends awake

  • sleep_awake_ratio that has the ratio of total time spent asleep to total time spent awake Then, subset msleep to only these new columns.

msleep |>
  mutate(bodywt_g = bodywt * 100,
         log_bodywt = log(bodywt),
         percent_day_awake = awake * 100 / 24,
         sleep_awake_ratio = sleep_total / awake) |>
  select(bodywt_g, log_bodywt, percent_day_awake, sleep_awake_ratio)
# A tibble: 83 × 4
   bodywt_g log_bodywt percent_day_awake sleep_awake_ratio
      <dbl>      <dbl>             <dbl>             <dbl>
 1   5000        3.91               49.6             1.02 
 2     48       -0.734              29.2             2.43 
 3    135        0.300              40               1.5  
 4      1.9     -3.96               37.9             1.64 
 5  60000        6.40               83.3             0.2  
 6    385        1.35               40               1.5  
 7   2049        3.02               63.8             0.569
 8      4.5     -3.10               70.8             0.412
 9   1400        2.64               57.9             0.727
10   1480        2.69               87.5             0.143
# ℹ 73 more rows

💻 Hands-On

What does the following code do? Modify the code to obtain the sample means of brainwt and sleep_cycle?

msleep |>
  drop_na(brainwt, sleep_cycle)

drop_na() removes rows (observations) containing missing values (NAs). If we put variable(s) in, it will remove only rows having missing values for those particular variable(s). Thus, after putting brainwt and sleep_cycle in drop_na(), we can find the sample means of these variables.

msleep |>
  drop_na(brainwt, sleep_cycle) |>
  summarize(mean_brainwt = mean(brainwt),
            mean_sleep_cycle = mean(sleep_cycle))
# A tibble: 1 × 2
  mean_brainwt mean_sleep_cycle
         <dbl>            <dbl>
1        0.125            0.431

Another way without using drop_na() is as follows:

msleep |>
  summarize(mean_brainwt = mean(brainwt, na.rm = TRUE),
            mean_sleep_cycle = mean(sleep_cycle, na.rm = TRUE))
# A tibble: 1 × 2
  mean_brainwt mean_sleep_cycle
         <dbl>            <dbl>
1        0.282            0.440

💻 Hands-On

Change column conservation to conservation_status and column vore to food_preference. You may find the rename() function useful.

msleep |>
  rename(conservation_status = conservation,
         food_preference = vore)
# A tibble: 83 × 11
   name    genus food_preference order conservation_status sleep_total sleep_rem
   <chr>   <chr> <chr>           <chr> <chr>                     <dbl>     <dbl>
 1 Cheetah Acin… carni           Carn… lc                         12.1      NA  
 2 Owl mo… Aotus omni            Prim… <NA>                       17         1.8
 3 Mounta… Aplo… herbi           Rode… nt                         14.4       2.4
 4 Greate… Blar… omni            Sori… lc                         14.9       2.3
 5 Cow     Bos   herbi           Arti… domesticated                4         0.7
 6 Three-… Brad… herbi           Pilo… <NA>                       14.4       2.2
 7 Northe… Call… carni           Carn… vu                          8.7       1.4
 8 Vesper… Calo… <NA>            Rode… <NA>                        7        NA  
 9 Dog     Canis carni           Carn… domesticated               10.1       2.9
10 Roe de… Capr… herbi           Arti… lc                          3        NA  
# ℹ 73 more rows
# ℹ 4 more variables: sleep_cycle <dbl>, awake <dbl>, brainwt <dbl>,
#   bodywt <dbl>

💻 Hands-On

What does the following code do? Can you think of a different way to obtain the same result?

msleep |>
  arrange(desc(sleep_cycle)) |>
  slice(1:5) 

The code sorts the data in decreasing lengths of sleep cycle and then keeps the first five rows. This can be done with less code using slice_max()

msleep |>
  slice_max(sleep_cycle, n = 5)
# A tibble: 5 × 11
  name    genus vore  order conservation sleep_total sleep_rem sleep_cycle awake
  <chr>   <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
1 Human   Homo  omni  Prim… <NA>                 8         1.9       1.5    16  
2 Chimpa… Pan   omni  Prim… <NA>                 9.7       1.4       1.42   14.3
3 Horse   Equus herbi Peri… domesticated         2.9       0.6       1      21.1
4 Brazil… Tapi… herbi Peri… vu                   4.4       1         0.9    19.6
5 Three-… Brad… herbi Pilo… <NA>                14.4       2.2       0.767   9.6
# ℹ 2 more variables: brainwt <dbl>, bodywt <dbl>

💻 Hands-On

Subset msleep to 3 carnivores with the smallest weights.

msleep |> 
  filter(vore == 'carni') |>
  slice_min(bodywt, n = 3)
# A tibble: 3 × 11
  name    genus vore  order conservation sleep_total sleep_rem sleep_cycle awake
  <chr>   <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
1 Northe… Onyc… carni Rode… lc                  14.5      NA            NA   9.5
2 Thick-… Lutr… carni Dide… lc                  19.4       6.6          NA   4.6
3 Slow l… Nyct… carni Prim… <NA>                11        NA            NA  13  
# ℹ 2 more variables: brainwt <dbl>, bodywt <dbl>

Second Part

💻 Hands-On

What does the following code do? Modify the code so that missing values are removed.

msleep |>
  select(vore) |>
  distinct()

The code keeps the vore column only and returns only unique (distinct) categories of this column. However, missing values NA are also considered a category. To avoid that, we should drop the missing values.

msleep |>
  select(vore) |>
  distinct() |>
  drop_na()
# A tibble: 4 × 1
  vore   
  <chr>  
1 carni  
2 omni   
3 herbi  
4 insecti

💻 Hands-On

Subset msleep to distinct rows of vore and conservation that have no missing values.

msleep |> 
  select(vore, conservation) |>
  distinct() |>
  drop_na()
# A tibble: 16 × 2
   vore    conservation
   <chr>   <chr>       
 1 carni   lc          
 2 herbi   nt          
 3 omni    lc          
 4 herbi   domesticated
 5 carni   vu          
 6 carni   domesticated
 7 herbi   lc          
 8 herbi   en          
 9 insecti lc          
10 herbi   cd          
11 carni   cd          
12 herbi   vu          
13 carni   en          
14 carni   nt          
15 insecti en          
16 omni    domesticated

💻 Hands-On

Subset msleep to only herbivores and then order the data by name.

msleep |>
  filter(vore == 'herbi') |>
  arrange(name)
# A tibble: 32 × 11
   name   genus vore  order conservation sleep_total sleep_rem sleep_cycle awake
   <chr>  <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
 1 Afric… Loxo… herbi Prob… vu                   3.3      NA        NA      20.7
 2 Arcti… Sper… herbi Rode… lc                  16.6      NA        NA       7.4
 3 Asian… Elep… herbi Prob… en                   3.9      NA        NA      20.1
 4 Brazi… Tapi… herbi Peri… vu                   4.4       1         0.9    19.6
 5 Chinc… Chin… herbi Rode… domesticated        12.5       1.5       0.117  11.5
 6 Cotto… Sigm… herbi Rode… <NA>                11.3       1.1       0.15   12.7
 7 Cow    Bos   herbi Arti… domesticated         4         0.7       0.667  20  
 8 Degu   Octo… herbi Rode… lc                   7.7       0.9      NA      16.3
 9 Donkey Equus herbi Peri… domesticated         3.1       0.4      NA      20.9
10 Easte… Tami… herbi Rode… <NA>                15.8      NA        NA       8.2
# ℹ 22 more rows
# ℹ 2 more variables: brainwt <dbl>, bodywt <dbl>

💻 Hands-On

Subset msleep to only species whose conservation status is least concern. Then, remove column conservation and all rows with missing values.

msleep |>
  filter(conservation == 'lc') |>
  mutate(conservation = NULL) |>
  drop_na()
# A tibble: 9 × 10
  name  genus vore  order sleep_total sleep_rem sleep_cycle awake brainwt bodywt
  <chr> <chr> <chr> <chr>       <dbl>     <dbl>       <dbl> <dbl>   <dbl>  <dbl>
1 Grea… Blar… omni  Sori…        14.9       2.3       0.133   9.1 0.00029  0.019
2 Less… Cryp… omni  Sori…         9.1       1.4       0.15   14.9 0.00014  0.005
3 Long… Dasy… carni Cing…        17.4       3.1       0.383   6.6 0.0108   3.5  
4 Nort… Dide… omni  Dide…        18         4.9       0.333   6   0.0063   1.7  
5 Big … Epte… inse… Chir…        19.7       3.9       0.117   4.3 0.0003   0.023
6 Euro… Erin… omni  Erin…        10.1       3.5       0.283  13.9 0.0035   0.77 
7 Labo… Ratt… herbi Rode…        13         2.4       0.183  11   0.0019   0.32 
8 East… Scal… inse… Sori…         8.4       2.1       0.167  15.6 0.0012   0.075
9 Thir… Sper… herbi Rode…        13.8       3.4       0.217  10.2 0.004    0.101

💻 Hands-On

Subset msleep to all the mammals who are generally more awake than asleep.

msleep |>
  filter(awake > sleep_total)
# A tibble: 52 × 11
   name   genus vore  order conservation sleep_total sleep_rem sleep_cycle awake
   <chr>  <chr> <chr> <chr> <chr>              <dbl>     <dbl>       <dbl> <dbl>
 1 Cow    Bos   herbi Arti… domesticated         4         0.7       0.667  20  
 2 North… Call… carni Carn… vu                   8.7       1.4       0.383  15.3
 3 Vespe… Calo… <NA>  Rode… <NA>                 7        NA        NA      17  
 4 Dog    Canis carni Carn… domesticated        10.1       2.9       0.333  13.9
 5 Roe d… Capr… herbi Arti… lc                   3        NA        NA      21  
 6 Goat   Capri herbi Arti… lc                   5.3       0.6      NA      18.7
 7 Guine… Cavis herbi Rode… domesticated         9.4       0.8       0.217  14.6
 8 Grivet Cerc… omni  Prim… lc                  10         0.7      NA      14  
 9 Star-… Cond… omni  Sori… lc                  10.3       2.2      NA      13.7
10 Afric… Cric… omni  Rode… <NA>                 8.3       2        NA      15.7
# ℹ 42 more rows
# ℹ 2 more variables: brainwt <dbl>, bodywt <dbl>

💻 Hands-On

Subset msleep to contain only carnivores with body weights greater than 50 kg. Then, arrange the data in ascending order of body weight. Finally, keep only columns name, bodywt, and brainwt in that order.

msleep |>
  filter(vore == 'carni', bodywt > 50) |>
  arrange(bodywt) |>
  select(name, bodywt, brainwt)
# A tibble: 8 × 3
  name                 bodywt brainwt
  <chr>                 <dbl>   <dbl>
1 Common porpoise        53.2  NA    
2 Gray seal              85     0.325
3 Caspian seal           86    NA    
4 Jaguar                100     0.157
5 Lion                  161.   NA    
6 Tiger                 163.   NA    
7 Bottle-nosed dolphin  173.   NA    
8 Pilot whale           800    NA    

💻 Hands-On

We want to subset msleep to all mammals whose brain weight is less than 2. However, the following code has an error. Fix the error in the code.

msleep |>
  select(bodywt, name) |>
  filter(brainwt < 2) 

The given code has an error, because brainwt was not selected and there is thus no brainwt to filter. There are two ways to fix.

# method 1: do include brainwt
msleep |>
  select(brainwt, bodywt, name) |>
  filter(brainwt < 2) 
# A tibble: 54 × 3
   brainwt  bodywt name                      
     <dbl>   <dbl> <chr>                     
 1 0.0155    0.48  Owl monkey                
 2 0.00029   0.019 Greater short-tailed shrew
 3 0.423   600     Cow                       
 4 0.07     14     Dog                       
 5 0.0982   14.8   Roe deer                  
 6 0.115    33.5   Goat                      
 7 0.0055    0.728 Guinea pig                
 8 0.0064    0.42  Chinchilla                
 9 0.001     0.06  Star-nosed mole           
10 0.0066    1     African giant pouched rat 
# ℹ 44 more rows
# method 2: filter observations first before selecting columns
msleep |>
  filter(brainwt < 2) |>
  select(brainwt, bodywt, name)
# A tibble: 54 × 3
   brainwt  bodywt name                      
     <dbl>   <dbl> <chr>                     
 1 0.0155    0.48  Owl monkey                
 2 0.00029   0.019 Greater short-tailed shrew
 3 0.423   600     Cow                       
 4 0.07     14     Dog                       
 5 0.0982   14.8   Roe deer                  
 6 0.115    33.5   Goat                      
 7 0.0055    0.728 Guinea pig                
 8 0.0064    0.42  Chinchilla                
 9 0.001     0.06  Star-nosed mole           
10 0.0066    1     African giant pouched rat 
# ℹ 44 more rows

💻 Hands-On

We want to subset msleep to show just the REM sleep amounts for carnivores, where data is arranged in order of genus. However, the following code has an error. Fix the error in the code.

msleep |>
  filter(vore == "carni") |>
  select(sleep_rem) |>
  arrange(genus)

The given code has an error, because genus was not selected when subsetting columns. It is better to arrange by genus before filtering rows or selecting columns.

msleep |>
  arrange(genus) |>
  filter(vore == "carni") |>
  select(sleep_rem)
# A tibble: 19 × 1
   sleep_rem
       <dbl>
 1      NA  
 2       1.4
 3       2.9
 4       3.1
 5       3.2
 6       1.3
 7       0.1
 8       1.5
 9       6.6
10      NA  
11      NA  
12      NA  
13      NA  
14      NA  
15       0.4
16      NA  
17      NA  
18      NA  
19       2.4

💻 Hands-On

What is the difference between these two code snippets?

# snippet 1
msleep |>
  summarize(mean_awake = mean(awake))

# snippet 2
msleep |>
  mutate(mean_awake = mean(awake))

In the given code, summarize() gives a single number showing the sample mean of the amount of time spent awake, while mutate() gives a whole column with a single value (sample mean of awake). To put it in another way, summarize() gives us summary statistics, while mutate() changes the column awake and returns the new data set

💻 Hands-On

Determine the average amount of time spent in REM sleep based on non-missing values.

Consider the following ways:

# method 1: drop NAs then calculate the sample mean
msleep |>
  select(sleep_rem) |>
  drop_na() |>
  summarize(mean_sleep_rem = mean(sleep_rem))
# A tibble: 1 × 1
  mean_sleep_rem
           <dbl>
1           1.88
# method 2: use the na.rm = TRUE option of mean()
msleep |>
  summarize(mean_sleep_rem = mean(sleep_rem, na.rm = TRUE))
# A tibble: 1 × 1
  mean_sleep_rem
           <dbl>
1           1.88

💻 Hands-On

Calculate the median body weight and the maximum brain weight of each vore group.

msleep |>
  select(vore, bodywt, brainwt) |>
  drop_na() |>
  group_by(vore) |>
  summarize(median_body_weight = median(bodywt),
            max_brain_weight = max(brainwt))
# A tibble: 4 × 3
  vore    median_body_weight max_brain_weight
  <chr>                <dbl>            <dbl>
1 carni                3.5              0.325
2 herbi                2.79             5.71 
3 insecti              0.075            0.081
4 omni                 0.9              1.32 

💻 Hands-On

Calculate the mean body weight of each combination of vore and conservation groups.

msleep |>
  select(bodywt, vore, conservation) |>
  drop_na() |>
  group_by(vore, conservation) |>
  summarize(mean_body_weight = mean(bodywt))
`summarise()` has grouped output by 'vore'. You can override using the
`.groups` argument.
# A tibble: 16 × 3
# Groups:   vore [4]
   vore    conservation mean_body_weight
   <chr>   <chr>                   <dbl>
 1 carni   cd                    800    
 2 carni   domesticated            8.65 
 3 carni   en                    163.   
 4 carni   lc                     27.8  
 5 carni   nt                    100    
 6 carni   vu                     80.3  
 7 herbi   cd                    900.   
 8 herbi   domesticated          195.   
 9 herbi   en                   1274.   
10 herbi   lc                      5.57 
11 herbi   nt                      0.546
12 herbi   vu                   2288.   
13 insecti en                     60    
14 insecti lc                      0.049
15 omni    domesticated           86.2  
16 omni    lc                      2.30 

💻 Hands-On

Count how many different taxonomic orders (see order) are in msleep and then sort the frequency in descending order.

msleep |>
  count(order) |>
  arrange(desc(n))
# A tibble: 19 × 2
   order               n
   <chr>           <int>
 1 Rodentia           22
 2 Carnivora          12
 3 Primates           12
 4 Artiodactyla        6
 5 Soricomorpha        5
 6 Cetacea             3
 7 Hyracoidea          3
 8 Perissodactyla      3
 9 Chiroptera          2
10 Cingulata           2
11 Didelphimorphia     2
12 Diprotodontia       2
13 Erinaceomorpha      2
14 Proboscidea         2
15 Afrosoricida        1
16 Lagomorpha          1
17 Monotremata         1
18 Pilosa              1
19 Scandentia          1

💻 Hands-On

Which group has the highest average body weight: herbivores or insectivores?

msleep |>
  filter(vore == 'herbi' | vore == 'insecti') |>
  group_by(vore) |>
  summarize(mean_body_weight = mean(bodywt))
# A tibble: 2 × 2
  vore    mean_body_weight
  <chr>              <dbl>
1 herbi              367. 
2 insecti             12.9