#' Saplings and Wildlings data processing
#'
#' Read an excel file with tabs containing number of observed saplings and wildings using plot-method survey.
#' Format of the worksheet should be:
#' First column containing Tags or Codes for Site/Location,
#' Second column are names/numbers of plots
#' First row containing labels for species while
#' Rest of the body should contain the observed number of the species per plot
#'
#'
#'
#'
#' @param excelpath Location of the excel file
#' @param sheet Name/s of the excel tabs where saplings and/or wildlings data are stored
#' @param clustlvl String to categorize at which spatial level of grouping the data analysis should be.
#'
#'
#' @return Outputs include
#' 'm.saps', a data frame for "Saplings" and/or 'm.wilds', a data frame for "Wildlings"
#'
#' @keywords saplings, wildlings
#'
#'
#'
#'
#' @export
data_sapwld<- function(excelpath = excelpath,
sheet = sheet,
clustlvl = clustlvl){
# Defines the `%>%` operator to the current environment
`%>%` <- dplyr::`%>%`
# Retrieves columns for selected cluster and SITE
clust.site<- plyr::ddply(m.data,
plyr::.(m.data[[clustlvl]], SITE),
plyr::summarize,
n = dplyr::n_distinct(`PLOT #`))
## Removes last column which is unnecessary
clust.site<- clust.site[,-3]
# Rename first column back to the cluster label
colnames(clust.site)[1]<- clustlvl
#####
## Checks if "Saplings" are included in the list
if("Saplings" %in% sheet){
## Reads "Sapling" sheet from excel and declares as a data frame
sap<- as.data.frame(readxl::read_excel(excelpath, sheet="Saplings", col_names = T))
## Replaces NAs of first column
sap[,1]<- zoo::na.locf(sap[,1])
## Replace NAs to zeroes
sap[is.na(sap)] <- 0
## Subset data to include numeric data with column sums not equal to zero
sap.num<- sap %>%
dplyr::select_if(~ !is.numeric(.) || sum(.) != 0)
## Transforms data into a long format for
sap.melt<- reshape::melt(sap.num, id=c(1,2))
## Renames column names of melted data frame so it can merge
colnames(sap.melt)<- c("SITE", "PLOT", "Species", "count")
## Merges melted data frame with cluster data frame by SITE codes
clust.melt<- merge(clust.site,sap.melt, by="SITE")
## Summarizes data per species
saps<- plyr::ddply(clust.melt,
plyr::.(clust.melt[[clustlvl]], Species),
plyr::summarize,
Counts = sum(count),
SE = round(sd(count) / sqrt((length(Species))), digits=2))
## Rename first column name to reflect cluster title
colnames(saps)[1]<- clustlvl
## Transforms data frame to long format
saps.melt<- reshape::melt(saps, id=c(clustlvl, "Species"))
## Merging cluster element name with variable name
saps.melt$variable<- paste(saps.melt[[clustlvl]], saps.melt$variable, sep = "_")
## Transforms data frame to wide format to tabulate easier
saps.spread<- reshape::cast(saps.melt, Species ~ variable, fill = "-")
## Outputs "m.saps" from the generated data frame
assign("m.saps", saps.spread, pos = .GlobalEnv)
}
## Checks if "Wildlings" are included in the list
if("Wildlings" %in% sheet){
## Reads "Wildlings" sheet from excel and declares as a data frame
wild<- as.data.frame(readxl::read_excel(excelpath, sheet="Wildlings", col_names = T))
## Replaces NAs of first column
wild[,1]<- zoo::na.locf(wild[,1])
## Replace NAs to zeroes
wild[is.na(wild)] <- 0
## Subset data to include numeric data with column sums not equal to zero
wild.num<- wild %>%
dplyr::select_if(~ !is.numeric(.) || sum(.) != 0)
## Transforms data into a long format for
wild.melt<- reshape::melt(wild.num, id=c(1,2))
## Renames column names of melted data frame so it can merge
colnames(wild.melt)<- c("SITE", "PLOT", "Species", "count")
## Merges melted data frame with cluster data frame by SITE codes
clust.melt<- merge(clust.site,wild.melt, by="SITE")
## Summarizes data per species
wilds<- plyr::ddply(clust.melt,
plyr::.(clust.melt[[clustlvl]], Species),
plyr::summarize,
Counts = sum(count),
SE = round(sd(count) / sqrt((length(Species))), digits=2))
## Rename first column name to reflect cluster title
colnames(wilds)[1]<- clustlvl
## Transforms data frame to long format
wilds.melt<- reshape::melt(wilds, id=c(clustlvl, "Species"))
## Merging cluster element name with variable name
wilds.melt$variable<- paste(wilds.melt[[clustlvl]], wilds.melt$variable, sep = "_")
## Transforms data frame to wide format to tabulate easier
wilds.spread<- reshape::cast(wilds.melt, Species ~ variable, fill = "-")
## Outputs "m.saps" from the generated data frame
assign("m.wilds", wilds.spread, pos = .GlobalEnv)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.