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:
<- read.csv("Chapter_8_files/bat_roosts.csv", row.names = 1, check.names = FALSE) bat_df
Add a row with column totals:
"UK",] <- colSums(bat_df) bat_df[
Add a column with row totals:
$All_Bat_Species <- rowSums(bat_df) bat_df
Create transposed data frame:
<- as.data.frame(t(bat_df)) bat_t_df
Write file:
write.table(bat_t_df,
file = "Chapter_8_files/bat_roosts_t.csv",
sep = ",", quote = FALSE, col.names = NA)
9.2 UK retail solution
Read in file:
<- read.csv("Chapter_8_files/UK_retail.tsv",
uk_retail_df sep = "\t", row.names = 1,
check.names = FALSE)
Create 2020 data frame: Read in file:
#Can index to get the desired columns
<- uk_retail_df[29:37,]
uk_retail_2020_df #Alternatively the tail() function can be used
#It is like head() but will get lowest rows
<- tail(uk_retail_df, n = 9) uk_retail_2020_df
Print food index phrases:
paste("The Food retail index for", row.names(uk_retail_2020_df),
"was", uk_retail_2020_df$Food,
sep = " ")
Total and mean rows:
"Total",] <- colSums(uk_retail_2020_df)
uk_retail_2020_df["Average",] <- colMeans(head(uk_retail_2020_df, n = 9)) uk_retail_2020_df[
Write out file:
write.table(uk_retail_2020_df, "Chapter_8_files/UK_retail_2020.tsv",
sep = "\t", col.names = NA, quote = FALSE)