Homework Problem 2, Due Wednesday September 9, 2009. --------------------------------------------------- ## (a) > Polynes = read.table(file= "http://www-users.math.umd.edu/~evs/s430.old/Data/nature02801-s2.dat", header=2, skip=2, nrow=80)[,c(1:5,7,9:11,15)] > unlist(lapply(Polynes,class)) Archip Islnd ForstRep Deforstn Area Isol_25 Elev LatitS "factor" "factor" "numeric" "numeric" "numeric" "numeric" "integer" "numeric" Rain Tephra "integer" "integer" ## (b) > Poly2 = is.na(Polynes[,3:10]) > dim(Poly2) [1] 80 8 > apply(Poly2,2,sum) ForstRep Deforstn Area Isol_25 Elev LatitS Rain Tephra 3 1 0 0 0 0 1 0 ## (c) > apply(Polynes[,3:10],2, function(col) round(c(mean= mean(col, na.rm=T), sd=sd(col, na.rm=T), IQR=IQR(col,na.rm=T), rng=c(range(col,na.rm=T) %*% c(-1,1))),3) ) ForstRep Deforstn Area Isol_25 Elev LatitS Rain Tephra mean 3.266 3.152 5914.239 188.963 1128.737 9.592 2165.329 1.837 sd 0.833 0.856 21525.631 501.988 961.030 15.956 1295.661 0.961 IQR 1.000 1.500 2104.075 130.000 950.000 12.158 1679.500 2.000 rng 4.000 3.000 150717.800 2998.000 4190.000 66.600 6600.000 2.000 ## (d) > charIsl = levels(Polynes$Islnd)[as.numeric(Polynes$Islnd)] ### as suggested by a student in the class: ### as.character(Polynes$Islnd) also works chars = nchar(charIsl) newnam = substr(charIsl,1,chars-1) lastchar = substr(charIsl,chars,chars) rowfrst = (newnam[1:79]==newnam[2:80]) & (lastchar[1:79]=="L") & (lastchar[2:80]=="W") > inds = sort(c((1:79)[rowfrst],1+(1:79)[rowfrst])) [1] 1 2 6 7 15 16 21 22 23 24 25 26 37 38 39 40 42 43 44 45 49 50 79 80 > length(inds)/2 [1] 12 #### 12 pairs > Polynes$Rain[inds[seq(2,24,by=2)]]-Polynes$Rain[inds[seq(1,23,by=2)]] [1] 1920 2500 1082 3278 -41 1236 2500 900 6600 600 900 583