Chapter 9 Files solutions

Before looking at these solutions keep in mind that there are many different ways to do the same thing in R. Therefore if your scripts are different than the ones below it does not mean they are wrong. As long as they produce the intended output they are correct.

9.1 Bats solution

Read in the file as a data frame:

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

Add a row with column totals:

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

Add a column with row totals:

bat_df$All_Bat_Species <- rowSums(bat_df) 

Create transposed data frame:

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

Write file:

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

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]

9.2 Honey bee colonies

Read in file:

bee_colonies_df <- read.csv("Chapter_8_files/honey_bee.tsv", 
                         sep = "\t", row.names = 1, check.names = FALSE)

Create 2017 & 2018 data frame: Read in file:

#Can index to get the desired columns
bee_colonies_2017_2018_df <- bee_colonies_df[9:16,]
#Alternatively the tail() function can be used
#It is like head() but will get lowest rows
bee_colonies_2017_2018_df <- tail(bee_colonies_df, n = 8)

Print Minnesota colony numbers:

paste("The number of colonies in Minnesota for", 
      row.names(bee_colonies_2017_2018_df),
      "was", bee_colonies_2017_2018_df$Minnesota, 
      sep = " ")

Mean row:

bee_colonies_2017_2018_df["Average",] <- colMeans(bee_colonies_2017_2018_df)

Write out file:

write.table(bee_colonies_2017_2018_df, 
            "Chapter_8_files/bee_colonies_2017_2018.tsv", 
            sep = "\t", col.names = NA, quote = FALSE)

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 state has the lowest average?
min_average <- min(bee_colonies_2017_2018_df["Average",])
states <- colnames(bee_colonies_2017_2018_df)
min_average_state <- states[
  bee_colonies_2017_2018_df["Average",] == min_average]
min_average_state
#Question 2: Which state has the highest minimum value of colonies?
bee_summary <- summary(bee_colonies_2017_2018_df[1:8,])
bee_summary
#Question 3: Which state had 20,000 colonies in Q3 of 2018 (2018-Q3)?
n <- 20000
column_logical_vec <- bee_colonies_2017_2018_df["2018-Q3",] == n
column_logical_vec
colnames(bee_colonies_2017_2018_df)[column_logical_vec]