# complete documentation for this script can be found at : # http://ucsb.piscoweb.org/~cburt/intertidal/workflow.html classes = c( 'factor', #site_code 'factor', #survey_rep 'integer', #year 'integer', #month 'integer', #day 'factor', #section 'factor', #transect 'factor', #location 'factor', #classification_code 'factor', #sampler_code 'factor', #zone_code 'integer' #count ) quad_data = read.csv( file="quad_numeric.csv", na.strings='.', head=TRUE, sep=",", colClasses = classes ) # this script will only handle the number of rows in nrow for the output. # as of 2006 this is 30,000 rows data = matrix(nrow=100000, ncol=7) #pointer for adding rows to the output matrix pointer = 0 sites = unique(quad_data$site_code); class_codes = levels(quad_data$class_code) num_of_sites = length(sites) site_count = 0 for(site in sites){ site_count = site_count + 1 message = paste('site', site_count, '/', num_of_sites) print(message) data_for_site = subset(quad_data, site_code == sites[site]); years = unique(data_for_site$year) num_of_years = length(years) year_counter = 0 for(y in years){ year_counter = year_counter + 1 message = paste('year', year_counter, '/', num_of_years) print(message) data_for_year = subset(data_for_site, year == y) #taking the taxon list from the year data. #So assuming that if a species does not have a zero datapoint, it has not been sampled cc = unique(data_for_year$class_code) for(c in cc){ data_for_taxon = subset(data_for_year, class_code == class_codes[c]) counts = data_for_taxon$count * 4 mean = mean(counts) if(!is.na(mean)){ pointer = pointer + 1 standard_deviation = sd(counts) n = length(counts) standard_error = standard_deviation / sqrt(n) data[pointer, ] = c( data_for_taxon$year[1], #year sites[site], #site class_codes[c], #class_code mean, #mean standard_deviation, #standard_deviation standard_error, #standard_error n #replicates, number of quads ) }else{ print('warning: mean is NA') } } } } write.table( na.omit(data), file="./quad_output.csv", row.names=FALSE, col.names = FALSE, sep="\t", quote=FALSE )