-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSTAMPS_Intro2R_OptionB_solutions.R
42 lines (38 loc) · 1.55 KB
/
STAMPS_Intro2R_OptionB_solutions.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# Solution to selective exercises of Option B
# Note that these are not the only possible solutions!
# Developing your own style is strongly encouraged!
# Differentially colour by january abundance
subset_data_frame <- data.frame("January"=abundances$JPA_Jan,
"February"=abundances$JPA_Feb,
"highabundance"=abundances$JPA_Jan > 4000)
names(subset_data_frame)
ggplot(subset_data_frame, aes(x = January, y = February, col = highabundance)) +
geom_point()
# use a for loop to find which taxa was most abundant
taxa<- rep(NA, dim(abundances)[1])
for (taxon in 1:dim(abundances)[1]) {
taxa[taxon] <- sum(abundances[taxon, ])
}
rownames(abundances)[which(taxa== max(taxa))]
# Ralstonia!
# Use a loop to loop through all of the samples,
# creating a list of the information corresponding to that sample.
# (name, location, month, relative abundance table, abundance table)
all_information <- list()
for (i in 1:dim(covariates)[1]) {
all_information[[i]] <- list()
for (j in 1:dim(covariates)[2]) {
all_information[[i]][[j]] <- covariates[i, j]
}
names(all_information[[i]]) <- colnames(covariates)
all_information[[i]]$abundances <- abundances[, i]
all_information[[i]]$relative <- relative_abundances[, i]
all_information[[i]]$taxonomy <- row.names(abundances)
}
names(all_information) <- covariates$SampleName
all_information$LOP_Jan
# Write a function that takes in an OTU table
# and returns the relative abundance table
counts_to_abundances <- function(otu) {
apply(otu, 2, function(x) x/sum(x))
}