## clear the workspace rm(list=ls()) ## -------------------------------------------------- ## Analyze vector of data: flying snakes ## 1. myvector <- c(0.9, 1.4, 1.2, 1.2, 1.3, 2.0, 1.4, 1.6) ## 2. hist(myvector, col='red') hist(myvector, col='red', right = FALSE) ## 3. x <- myvector*2*pi ## 4. sum(x)/length(x) ## this calculates the mean ## let's wrap this into a function my.mean <- function(vec) { mm <- sum(vec)/length(vec) return(mm) } x.mean <- my.mean(x) x.mean ## and I can also apply this function to the original data my.mean(myvector) ## 5. mean(x) ## 6. ## ## my own variance function my.var <- function(vec) { vv <- sum((x-x.mean)^2)/(length(x)-1) return(vv) } x.var <- my.var(vec) x.var ## my own sd function my.sd <- function(vec) { vv <- my.var(vec) return(sqrt(vv)) } x.sd <- my.sd(vec) x.sd ## 7. var(x) sd(x) ## 8. median(x) ## 9. sd(x)/sqrt(length(x)) ## -------------------------------------------------- ## Anolis lizards in a data frame ## 4. mydata <- read.csv('https://www.sfu.ca/~lmgonigl/materials-qm/data/anolis.csv') ## 5. str(mydata) class(mydata$Island) class(mydata) ## 6. head(mydata) head(mydata, 20) tail(mydata) ## 7. mydata <- read.csv('https://www.sfu.ca/~lmgonigl/materials-qm/data/anolis.csv', stringsAsFactors=TRUE) class(mydata$Island) ## 8. ## ## Note that, as an alternative to the $ sign, you can access columns ## by entering their name, in quotes in the 'column' position of the ## data.frame, as I do below. I prefer this format to the $-symbol, ## but both are ok, and I use them interchangeably. levels(mydata[,'Ecomorph']) ## 9. table(mydata[,'Ecomorph']) which(mydata[,'Ecomorph']=='Trunk-Crown ') ## 10. mydata[mydata[,'Ecomorph']=='Trunk-Crown ','Ecomorph'] <- 'Trunk-Crown' table(mydata[,'Ecomorph']) ## 11. mydata[,'Ecomorph'] <- droplevels(mydata[,'Ecomorph']) levels(mydata[,'Ecomorph']) ## 12. anolis <- read.csv('https://www.sfu.ca/~lmgonigl/materials-qm/data/anolis.csv', stringsAsFactors=TRUE, na.strings=c('', 'NA'), strip.white=TRUE) ## 13. table(anolis[,'Ecomorph']) ## 14. table(anolis[,'Ecomorph'], useNA = 'ifany') ## 15. table(anolis[,'Island']) ## 16. length(grep('Cuba', anolis[,'Island'])) ## 17. big.4 <- c('Cuba', 'Hispaniola', 'Jamaica', 'Puerto Rico') ## write a function to get the ecomorphs from one island get.for.one.island <- function(s) { table(anolis[grep(s, anolis[,'Island']), 'Ecomorph']) } ## test it get.for.one.island('Cuba') get.for.one.island('Hispaniola') ## do all at once big.4.tab <- sapply(big.4, get.for.one.island) big.4.tab ## transpose the table (switch rows and columns) for a more natural ## presentation t(big.4.tab) ## this works because no single species appears on multiple big islands ## 18. ## there is lots of syntax we haven't covered in here... we'll learn ## these as we go tab.not.big.4 <- table(anolis[!(anolis[,'Island'] %in% big.4),'Ecomorph']) names(tab.not.big.4)[which.max(tab.not.big.4)]