Chapter 1 READING DATA 1)Typing data x<-c( , , , , ) 2)Reading a file with one data column x<-scan('e:datafile.dat') 3) Reading a file with several columns x<-read.table('e:datafile.dat') CHAPTER 2 1.- For Table 2.1 and Figure 2.3 cells<-c(0,2,2,3,3,4,4,0,0,2,0,1,3,2,2,1,4,2,0,1,2,4,2,0,2,2, 0,4,0,1,1,1,3,0,1,4,1,4,5,3,0,3,0,0,3,2,2,1,2,1) table(cells) ## Figure 2.3a Dotplot stripchart(cells,method="stack", offset=1, at=0, pch=19,cex=2, main='DOTPLOT') ## Figure 2.3 b Frequency plot abnormalcells<-c(0,1,2,3,4,5) frequencies<-c(12,10,13,7,7,1) plot(abnormalcells,frequencies,'h',main='FREQUENCIES') 2. STEM-AND-LEAF DISPLAY page 30, HISTOGRAM , BOXPLOT area<-c(57.78, 57.89, 59.95, 62.68, 71.07, 73.46, 82.25, 65.93, 67.39, 66.32, 62.60, 55.94, 61.57, 59.39, 73.18, 61.04, 63.66, 59.12, 57.32, 62.36, 59.60, 58.93, 64.93, 64.61, 77.60, 61.47, 61.21, 59.37, 61.56, 55.61, 59.89, 66.74, 74.64, 62.84) stem(area) hist(area) boxplot(area) 3. Figure 2.4 HISTOGRAM AND BOXPLOT IN ONE FIGURE ## to read the data area<-c(57.78, 57.89, 59.95, 62.68, 71.07, 73.46, 82.25, 65.93, 67.39, 66.32, 62.60, 55.94, 61.57, 59.39, 73.18, 61.04, 63.66, 59.12, 57.32, 62.36, 59.60, 58.93, 64.93, 64.61, 77.60, 61.47, 61.21, 59.37, 61.56, 55.61, 59.89, 66.74, 74.64, 62.84) stem(area) ## to get the stem-and-leaf display summary(area) ## calculates five-number summary and mean par(mfcol=c(1,2))## 1 row and 2 columns of plots hist(area,col='khaki', main='a. Histogram') boxplot(area,col='coral',main='b. Boxplot') 4. CALCULATING BASIC STATISTICS x<-c(57.78, 57.89, 59.95, 62.68, 71.07, 73.46, 82.25, 65.93, 67.39, 66.32, 62.60, 55.94, 61.57, 59.39, 73.18, 61.04, 63.66, 59.12, 57.32, 62.36, 59.60, 58.93, 64.93, 64.61, 77.60, 61.47, 61.21, 59.37, 61.56, 55.61, 59.89, 66.74, 74.64, 62.84) summary(area) ## calculates five-number summary and mean sd(x) ## Calculates standard deviation var(x) ## Calculates the variance sd(x)/mean(x) ## Calculates the coefficient of variation ## Calculates mean absolute deviation with respect to the mean dev<-abs(x-mean(x)) mean(dev) 5. Change of units. Figure 2.14 par(mfcol=c(1,2)) ## puts both histograms in one figure Celsius<-scan('e:tempsea.dat') ## reads data file in Celsius Fahrenheit<-(Celsius*9/5)+32 ## transforms to Farenheit a<-c(14,16,18,20,22,24) ## endpoints for intervals in C b<-(a*9/5)+32 ## endpoints for intervals in F hist(Celsius,breaks=a,col='palegreen3') # histogram hist(Fahrenheit,breaks=b,col='lightcyan3') # histogram 6. Side by side boxplots- Figure 2.22 and statistics by groups length<-c(11.2,13.0,12.5,10.0,11.3, 7.0,10.0,10.2,10.6,6.7,9.3, 11.0,12.8,9.5,12.2,13.5,13.1,11.3,12.2,14.2,7.6,5.8,7.7, 9.0, 8.1,8.6,6.3,7.5,8.6,8.7,7.8,6.5,6.1,8.0,7.5,8.0,7.0,6.2,7.9,8.2) type<-c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2) boxplot(length~type,main='Sycamore and Sugar Maple leaves ', ylab='length (cm)', col='lightgreen') ## Calculating statistics by group by(length,type,mean); by(length,type,median) by(length,type,sd) 7. Figure 2.23 ## read a data file with 2 or more columns using read.table ## columns have no names thus header=FALSE gene<-read.table('e:onegene.dat',header=FALSE) ## objects are formed with each column intensity<-gene[,1] ## all rows of column 1 subtype<-gene[,2] ## all rows of column 2 ## produce side by side boxplots for the gene expression example boxplot(intensity~subtype, main='Intensity of expression of one gene', xlab='Subtype of tumor', ylab='Intensity' ,col='cadetblue2') 8. Dotplots by group- Figure 2.24 treatment<-c(2,2,2.4,1.8,1.9,1.8,2,1.5,1.9,2.1) control<-c(0.1,0,0.1,0.1,0,0.2,0.2,0.1,0.1,0.1) a<-c(0,2.5) ## entering the endpoints of the x axis par(mfcol=c(2,1)) ## 2 plots in the same figure ## cex controls the size of symbols ## offset controls the vertical space between symbols ## pch=19 indicates dots instead of squares ## xlim controls the endpoints of the x axis ## stripchart prepares the dotplot stripchart(treatment,method='stack',offset=0.5,at=0,cex=1, pch=19,xlim=a, main='a. Treatment',xlab='difference=after-before' ) stripchart(control,method='stack',offset=0.5,at=0,cex=1, pch=19,xlim=a, main='b. Control', xlab='difference=after-before') 9. Plotting time series- Figure 2.26 activity<-scan('e:onebeesu.dat') ## reads the data ts.plot(activity) ## plots the time series ## commands for Figure 2.25 temp<-scan('e:tempsea.dat') ## reads data temperature<- ts(temp, start = c(1956, 1), frequency = 12) ##dates t<-rep(seq(1,12,by=1),30) ## creates labels for months par(mfrow=c(2,1)) ## to plot two figures together ts.plot(temperature, main='a. timeplot') boxplot(temperature~t, main='b. boxplots by month') 10. Survival plot in Figure 2.28 nindi<-200 ## how many flies we start with ## we type in the data of number of dead flies each day x<-c(0,1,2,0,2,1,5,1,4,2,5,4,3,7,6, 6,18,11,18,31,21,17,0,23,6,1,3,0,0,0) ## NO MORE INPUT IS NEEDED n=length(x) ## calculates the number of days day<-seq(1,n,by=1) ## creates the values for 'day' cum<-cumsum(x) ## accumulates the values surv<-nindi-cum ## calculates the number of survivors propsurv<-surv/nindi ## calculates proportion of survivors plot(day,propsurv,'s',col='red',ylim=c(0,1)) abline(0.5,0) ## adds the horizontal line ## END HERE if there is only one group 11. SCATTERPLOTS Figure 2.29 Altitude<-c(0,1840,2200,2200,5000,5200,5750,7400,8650,10740,12000, 12200,12300,14200,14800,14900,17500) RBC<-c(4.93,4.75,5.40,4.65,5.42,6.55,5.99,5.39,5.44,5.82,7.50,5.67, 6.31,7.05,6.46,6.66,7.37) plot(Altitude,RBC) ## gets scatterplot cor(Altitude,RBC)## calculates Pearson's correlation r 12. Matrix of scatter plots and stars plots (Figures 2.32 and 2.33) ## reads the data file with several columns birdswn<-read.table('e:/DATdatafiles/birds20wn2.dat') birds<-birdswn[,2:6] ## creates object birds with numerical values stars(birds,labels=birdswn[,1]) ## makes stars, puts labels pairs(birds) ## produces matrix of scatterplots 13. BAR CHART AND PIE CHART Figure 2.34 par(mfcol=c(1,2)) ## 1 row and 2 columns of plots g<-c(60,100) ## enter frequencies names(g)<-c("Round ", "Sickle ") barplot(g, main= "Erythrocytes by shape ", col= "bisque") pie(g,c("round","sickle")) 14. CLUSTERED BAR CHARTS Figure 2.35 ## clustered bars charts ## next we enter the rows of data A<-matrix(c(77,49,45,39,45,36,43,101,75,51),nrow=5,ncol=2) ## enter the names of the categories rnames<-c('0-20 ', '20-40 ','40-60 ', '60-80', '80-100') ## this command prepares the plot barplot(A,beside=TRUE, names=c('Random sites','Habitat sites'), xlab='% cover',main='Understory vegetation',legend=rnames) 15. STACKED BAR CHARTS Figure 2.36 ## clustered and stacked bars charts par(mfcol=c(1,2)) ## 2 plots appear side by side ## next we enter the 2 columns of data or percentages A<-matrix(c(35.7,7.3,2.5,54.5,41.03,10.15,2.5,46.32),nrow=4,ncol=2) rnames<-c('A ', 'B ','AB ', '0') ## the only difference is in the BESIDE option of the barplot barplot(A,beside=TRUE, names=c('ulcers ', 'no ulcers '), main='Blood type',ylab='percent',legend=rnames) barplot(A,beside=FALSE,names=c('ulcers ', 'no ulcers '), main='Blood type',ylab='percent') 16. Exercise 7 in chapter 2 (page 93) cell<-c(6.2,6.0,6.7,7.4,6.0,6.4 ,5.9,5.9,6.1,6.2,6.5,6.1,6.2, 6.6,7.2,7.5,5.3,6.0,5.9,6.3,7.0,6.9,5.1,6.1) nucleus<-c(2.2,2.1,2.7,3.2,2.1,2.1,2.3,2.4,2.3,2.2,2.7,2.4 ,2.4,2.4,3.0,2.9,2.0,2.3,2.2,2.4,2.3,2.4,2.1,2.1) plot(cell,nucleus) n<-length(cell) ## to count the number of observations ((n-1)/n)*cor(cell,nucleus) 17. Exercise 8 in Chapter 2 (page 94) height<-c(63, 66, 67, 61.5, 65, 67, 64, 64.8, 66.5, 64.5, 67, 68, 67, 63, 65, 63, 66.5, 66, 65.5, 63, 65, 68, 62) foot <-c(8, 8, 9.5, 8, 7.1, 8.5, 8.35, 8, 10.1, 8.5, 9.5, 7.5, 8.1, 8, 9.5, 7, 9.50, 9.50, 9.50, 8.5, 8, 9, 7) handspan<-c( 8, 7.5, 7.5, 6.2, 6.2, 8, 6.40, 7, 8.4, 8.5, 8.25, 7.4, 6.9, 7.5, 8.5, 6, 8.5, 7, 7.25, 7.5, 7, 8, 7.5) cor(height,foot) cor(foot,handspan) cor(height,foot) 18. Exercise 9 petiole<-c(6.2, 3.6, 5.3, 5.6, 5.2, 6, 4.2, 6.1, 5.3, 6.9, 4.3, 4.8, 5.5, 5, 5.2, 4.9, 5.5, 4.6, 5.9, 6.4) leaf<-c(0.587,0.235,0.480,0.738,0.587,0.793,0.401,0.611,0.617, 0.578,0.373,0.327,0.281,0.528,0.399,0.543,0.413,0.323,0.544,0.577) 19. Project 1 , page 95 ## reads the data file hens<-read.table('e:/DATdatafiles/hens.dat',header=FALSE) ## creates objects with each variable species<-hens[,1]; mass<-hens[,2]; wing<-hens[,3]; tarsus<-hens[,4]; tail<-hens[,5]; clutch<-hens[,6]; eggmass<-hens[,7] ## create a data frame with some variables for later use. body<-data.frame(mass,wing,tarsus,tail) stars(body) 20. Project 2, page 96 ## reads the data file birds<-read.table('e:/DATdatafiles/LybiidaeMales.dat', header=FALSE) ## creates objects with each variable species<-birds[,1]; mass<-birds[,2]; tarsus<-birds[,3] bill<-birds[,4]; wing<-birds[,5]; tail<-birds[,6] ## forms a data frame that does not include the species number body<-data.frame(mass,tarsus,bill,wing,tail) stars(body) pairs(body) 21. Project 4 , page 97 mydata<-read.table('e:/DATdatafiles/fiveleaves.dat', header=FALSE) species<-mydata[,1]; length<-mydata[,2] width<-mydata[,3] ; petiole<-mydata[,4] mass<-mydata[,5] 22. Project 5, page 98 bones<-read.table('e:/DATdatafiles/BMD.dat', header=FALSE) age<-bones[,1] lumbar1<-bones[,2]; lumbar2<-bones[,3] lumbar3<-bones[,4]; lumbar4<-bones[,5] LFNeck<-bones[,6]; LTotHip<-bones[,7] RFNeck<-bones[,8]; RTotHip<-bones[,9]