Chapter 8 Bat roost exercise

First we will look at the file bat_roosts.csv. This contains information on the max number of roosts for different Bat species in different UK regions.

The data is from: "Bat Conservation Trust 2020. Roost Count peak counts summary data". Available from https://www.bats.org.uk/our-work/national-bat-monitoring-programme/reports/nbmp-annual-report

Solutions are in the expandable boxes. Try your best to solve each challenge but use the solutions for help if you would like. Even if your method works it can be good to check the solution as there are many ways to do the same thing in R

8.1 Bat roost challenges

Bat roost challenge 1

Read in the file "bat_roosts.csv" as a data frame variable called "bat_df". Ensure the row names contain the Regions (Channel Islands, East Midlands, etc.).

Read in the file as a data frame:

bat_df <- read.csv("Chapter_8_files/bat_roosts.csv", 
                   row.names = 1, check.names = FALSE)

Bat roost challenge 2

Inspect the variable and ensure there are only numerics within the data frame with all strings only being in column and row names.

Bat roost challenge 3

Add a row to "bat_df" called "UK" that contains the totals for each Species.

Add a row with column totals:

bat_df["UK",] <- colSums(bat_df) 

Bat roost challenge 4

Add a column to "bat_df" called "All_Bat_Species" that contains the totals for each Region.

Create transposed data frame: Add a column with row totals:

bat_df$All_Bat_Species <- rowSums(bat_df) 

Bat roost challenge 5

Create a transposed data frame of "bat_df" called "bat_t_df".

Create transposed data frame:

bat_t_df <- as.data.frame(t(bat_df)) 

Bat roost challenge 6

Write the data frame "bat_t_df" to a comma separated file called "bat_roosts_t.csv". Ensure there are no quotes surrounding the row or column names.

Write file:

write.table(bat_t_df, file = "Chapter_8_files/bat_roosts_t.csv", 
            sep = ",", quote = FALSE, col.names = NA)

8.2 Bat roost MCQs

Fantastic! I hope those challenges were engaging and improved your experience working with files and data frames.

Attempt the following MCQs using your final "bat_t_df" object.

  1. Which region has no roosts?
  2. Which region has the largest amount of Myotis brandtii roosts (excluding UK)?
  3. Which Bat species has the highest number of roosts across the UK?
  4. Which Bat species has the lowest number of roosts across the UK?

You can figure out the MCQs by viewing the data frame. However, you can also figure them out with R code. Below are commands to get the answers for the MCQs. I will let you decipher them yourself.

#Question 1: Which region has no roosts?
col_names_logical_vec <- bat_t_df["All_Bat_Species",] == 0
colnames(bat_t_df)[col_names_logical_vec]
#Question 2: Which region has the largest amount of Myotis brandtii roosts?
species <- "Myotis brandtii"
df_wo_total_species_and_uk <- bat_t_df[
  1:(nrow(bat_t_df)-1),1:(ncol(bat_t_df)-1)]
max_colonies <- max(df_wo_total_species_and_uk[species,])
col_names_logical_vec <- df_wo_total_species_and_uk[species,] == max_colonies
colnames(df_wo_total_species_and_uk)[col_names_logical_vec]
#Question 3: Which Bat species has the highest number of roosts across the UK?
max_uk <- max(bat_t_df[1:(nrow(bat_t_df)-1),"UK"])
row_names_logical_vec <- bat_t_df$UK == max_uk
row.names(bat_t_df)[row_names_logical_vec]
#Question 4: Which Bat species has the lowest number of roosts across the UK?
min_uk <- min(bat_t_df[1:(nrow(bat_t_df)-1),"UK"])
row_names_logical_vec <- bat_t_df$UK == min_uk
row.names(bat_t_df)[row_names_logical_vec]