Walkthrough of how to go from data on invidiual observations to aggregated and summarized data.
Functions Use
dat <- read.csv("miller_real_data_update.csv" )
dat$species<- gsub("^ ","",dat$species)
dat$species<- gsub(" $","",dat$species)
This is what does the magic
library(reshape2)
dat$i <- 1:dim(dat)[1]
dat.melt <- melt(data = dat,
id.vars =c("i","wetland","section","species"),
measure.vars = c("wet","dry"),
variable.name = "sample.type")
names(dat.melt)[6] <- "mass"
This sums up the total mass for each species. First step done!
dat2 <- dcast(data = dat.melt,
formula = wetland + section +
species ~ sample.type,
value.var = "mass",
fun.aggregate = sum)
Load the list of species with what type of plant it is (wetland, uplad etc)
spp.list <- read.csv("species_list_update.csv" )
names(dat2)[3] <- "spp"
This categorizes each species in the data appropriatly based on what is in the species list
dat3 <- merge(dat2, spp.list, by = "spp")
dat4 <- dcast(data = dat3,
formula = wetland + section ~ type,
value.var = "dry",
fun.aggregate = sum)
and save it
write.csv(dat4, file = "summarized_data.csv")