Exercise16

Maggie Hallerud

December 5, 2017

Import leks CSV and .RData

setwd("~/WILD4580/data/exercise_dat") #set working directory
load("Lek_summary.RData") #import R data object created previously
leks<-read.csv("gsg_leks.csv") #import CSV dataset
str(leks) #check out CSV structure
## 'data.frame':    600 obs. of  5 variables:
##  $ year       : int  2006 2007 1981 1982 1983 1985 1988 1989 1990 1991 ...
##  $ disturbance: Factor w/ 2 levels "N","Y": 1 1 1 1 1 1 1 1 1 1 ...
##  $ lek_id     : Factor w/ 47 levels "3T","bare","bcmtn",..: 1 1 2 2 2 2 2 2 2 2 ...
##  $ complex    : Factor w/ 5 levels "","3corners",..: 5 5 2 2 2 2 2 2 2 2 ...
##  $ tot_male   : int  3 17 4 7 2 5 0 5 0 0 ...

Add number of samples per year/complex combination

counts<-aggregate(leks$lek_id, by=list(leks$complex,leks$year),FUN=length) #calculate counts
names(counts)<-c("complex","year","n"); head(counts) # write headings to counts
##    complex year n
## 1  Diamond 1967 2
## 2  Diamond 1969 2
## 3 3corners 1970 1
## 4  Diamond 1970 4
## 5 3corners 1971 3
## 6  Diamond 1971 4
# save all statistics to .RData object
save(counts, max3, min3, mean3, sd3, file="~/WILD4580/data/exercise_dat/Lek_summary.RData")

Merge statistics into single data object, and export as a .csv file

#merge by column and save as object
lek_stats = cbind(mean3, sd3[3], min3[3], max3[3], counts[3]) 
str(lek_stats); head(lek_stats) #double check
## 'data.frame':    119 obs. of  7 variables:
##  $ complex      : Factor w/ 5 levels "","3corners",..: 5 5 2 5 2 5 2 5 2 5 ...
##  $ year         : int  1967 1969 1970 1970 1971 1971 1972 1972 1973 1973 ...
##  $ mean_tot_male: num  67 49 26 26.2 12.3 ...
##  $ sd_tot_male  : num  48.1 28.3 NA 21.1 8.5 ...
##  $ min_tot_male : int  33 29 26 3 6 0 0 0 0 0 ...
##  $ max_tot_male : int  101 69 26 51 22 33 26 66 32 38 ...
##  $ n            : int  2 2 1 4 3 4 3 5 4 7 ...
##    complex year mean_tot_male sd_tot_male min_tot_male max_tot_male n
## 1  Diamond 1967      67.00000   48.083261           33          101 2
## 2  Diamond 1969      49.00000   28.284271           29           69 2
## 3 3corners 1970      26.00000          NA           26           26 1
## 4  Diamond 1970      26.25000   21.093048            3           51 4
## 5 3corners 1971      12.33333    8.504901            6           22 3
## 6  Diamond 1971      15.25000   14.407753            0           33 4
write.csv(lek_stats, file = "lek_statistics.csv") #write data object into CSV file