Import your data

data <- read_csv("../00_data/MKmyData1.csv")
## Rows: 101 Columns: 17
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (8): id.on.tag, animal.name, scientific.name, tag.deployment.start, tag....
## dbl (5): Column1, prey.per.month, hours.indoor.per.day, cats.in.house, age
## lgl (4): hunt, dry.food, wet.food, other.food
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Chapter 15

Create a factor

reproduction.condition_levels <- c("Neutered", "Spayed", "NA")

reproduction.condition_levels %>%
    factor(levels = reproduction.condition_levels)
## [1] Neutered Spayed   NA      
## Levels: Neutered Spayed NA

Modify factor order

Make two bar charts here - one before ordering another after

# Unordered Factor levels
age_by_animal.name <- data %>%
    
    group_by(animal.name) %>%
    summarise(
        avg_age = mean(age, na.rm = TRUE)
    )

age_by_animal.name
## # A tibble: 101 × 2
##    animal.name avg_age
##    <chr>         <dbl>
##  1 Abba              3
##  2 Alfie             3
##  3 Amber             4
##  4 Ares              3
##  5 Athena            3
##  6 Balu              7
##  7 Barney           11
##  8 Beanie            4
##  9 Bear              6
## 10 Bella             3
## # ℹ 91 more rows
# Plot
age_by_animal.name %>%
    
    ggplot(aes(x = avg_age, y = animal.name)) +
    geom_point() 
## Warning: Removed 1 rows containing missing values (`geom_point()`).

# Ordered Factor levels
age_by_animal.name %>%
    
    ggplot()

# Ran into multiple errors trying to create chart that I was not sure how to fix

Modify factor levels

Show examples of three functions:

# Recode
data %>%

    mutate(study.location_rev = fct_recode(study.location, "United Kingdom" = "UK")) %>%
    select(study.location, study.location_rev) %>%
    filter(study.location == "UK")
## # A tibble: 101 × 2
##    study.location study.location_rev
##    <chr>          <fct>             
##  1 UK             United Kingdom    
##  2 UK             United Kingdom    
##  3 UK             United Kingdom    
##  4 UK             United Kingdom    
##  5 UK             United Kingdom    
##  6 UK             United Kingdom    
##  7 UK             United Kingdom    
##  8 UK             United Kingdom    
##  9 UK             United Kingdom    
## 10 UK             United Kingdom    
## # ℹ 91 more rows
# Collapse multiple levels into one
data %>%
    
    mutate(reproductive.condition_col = fct_collapse(reproductive.condition, "Not Sure" = c("Spayed"))) %>%
    select(reproductive.condition, reproductive.condition_col) %>%
    filter(reproductive.condition != "Neutered")
## # A tibble: 43 × 2
##    reproductive.condition reproductive.condition_col
##    <chr>                  <fct>                     
##  1 Spayed                 Not Sure                  
##  2 Spayed                 Not Sure                  
##  3 Spayed                 Not Sure                  
##  4 Spayed                 Not Sure                  
##  5 Spayed                 Not Sure                  
##  6 Spayed                 Not Sure                  
##  7 Spayed                 Not Sure                  
##  8 Spayed                 Not Sure                  
##  9 Spayed                 Not Sure                  
## 10 Spayed                 Not Sure                  
## # ℹ 33 more rows
# Lump small levels into other levels
data %>% count(animal.name)
## # A tibble: 101 × 2
##    animal.name     n
##    <chr>       <int>
##  1 Abba            1
##  2 Alfie           1
##  3 Amber           1
##  4 Ares            1
##  5 Athena          1
##  6 Balu            1
##  7 Barney          1
##  8 Beanie          1
##  9 Bear            1
## 10 Bella           1
## # ℹ 91 more rows
data %>% mutate(animal.name_lump = fct_lump(animal.name)) %>% distinct(animal.name_lump)
## # A tibble: 101 × 1
##    animal.name_lump
##    <fct>           
##  1 Tommy           
##  2 Athena          
##  3 Ares            
##  4 Lola            
##  5 Maverick        
##  6 Coco            
##  7 Charlie         
##  8 Jago            
##  9 Morpheus        
## 10 Nettle          
## # ℹ 91 more rows
  • fct_recode
  • fct_collapse
  • fct_lump

Chapter 16

No need to do anything here.