Introduction to R statistical software and programming language _______________________________________________________________ R version 2.11.1 (2010-05-31) Copyright (C) 2010 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # This is a comment. R ignores anything that follows the pound sign (#). > # This is a transcript of introductory R commands. > # To learn more about R, including how to download > # your own copy of R, go to > # http://cran.R-project.org > # Douglas fir dbh data, from ESCI 340 class, Fri 10 Jan. 2014 > # Trees growing near edge of forest > dbh.edge <- c(87.5, 95.1, 75, 88.3, 83, 92.7, 88.5, 96, 118.9, 62.5) > dbh.edge [1] 87.5 95.1 75.0 88.3 83.0 92.7 88.5 96.0 118.9 62.5 > length(dbh.edge) # check that 10 measurements entered [1] 10 > # Trees growing in interior of forest > dbh.int <- c(115.2, 80, 91.6, 63, 112, 107, 52.1, 102.4, 99, 68.0) > dbh.int [1] 115.2 80.0 91.6 63.0 112.0 107.0 52.1 102.4 99.0 68.0 > length(dbh.int) # check that 10 measurements entered [1] 10 > # How to edit variables > dbh.int <- dbh.int[1:9] # Keep only 1st 9 measurements > dbh.int [1] 115.2 80.0 91.6 63.0 112.0 107.0 52.1 102.4 99.0 > # alternatively, could type "dbh.int <- dbh.int[-10]" > dbh.int <- c(dbh.int, 68.0) # restore 10th measurement > dbh.int [1] 115.2 80.0 91.6 63.0 112.0 107.0 52.1 102.4 99.0 68.0 > dbh.int[10] <- 2 # change 10th measurement > dbh.int [1] 115.2 80.0 91.6 63.0 112.0 107.0 52.1 102.4 99.0 2.0 > dbh.int[10] <- 68.0 # restore 10th measurement > dbh.int [1] 115.2 80.0 91.6 63.0 112.0 107.0 52.1 102.4 99.0 68.0 > # Combine edge and interior measurements into a single variable > dbh.all <- c(dbh.edge, dbh.int) > dbh.all [1] 87.5 95.1 75.0 88.3 83.0 92.7 88.5 96.0 118.9 62.5 115.2 80.0 [13] 91.6 63.0 112.0 107.0 52.1 102.4 99.0 68.0 > # Plot histogram of combined measurements > hist(dbh.all) > # Create 3-panel plot, to show histograms of combined measurements > # and histograms for edge and interior samples > par(mfrow = c(3,1)) > hist(dbh.all) > hist(dbh.edge) > hist(dbh.int) > # Plot all 3 histograms on same x-axis range (50 to 120) > hist(dbh.all, xlim=c(50, 120)) > hist(dbh.edge, xlim=c(50, 120)) > hist(dbh.int, xlim=c(50, 120)) > # Add informative figure title and x-axis labels > hist(dbh.all, xlim=c(50, 120), xlab="DBH, all trees (m)", + main="Histogram of tree diameters") > hist(dbh.edge, xlim=c(50, 120), xlab="DBH, edge trees (m)", main="") > hist(dbh.int, xlim=c(50, 120), xlab="DBH, interior trees (m)", main="") > # Change histogram intervals from default (10m wide) to 20m width > # First, define interval limits, or "breaks" > x.20 <- c(40, 60, 80, 100, 120) > # Equivalently, use sequence command: > x.20 <- seq(from=40, to=120, by=20) > x.20 [1] 40 60 80 100 120 > # Plot histograms with specified intervals > hist(dbh.all, xlim=c(40, 120), xlab="DBH, all trees (m)", main="", + breaks=x.20) > # Histogram interval widths can vary, > # e.g., finer resolution for small values: > x.v <- c(seq(from=40, to=60, by=5), seq(from=80, to=120, by=20)) > x.v [1] 40 45 50 55 60 80 100 120 > hist(dbh.all, xlim=c(40, 120), xlab="DBH, all trees (m)", main="", + breaks=x.v) > # ******************************************************* > # Overlay normal curve on histogram: > dbh.edge <- c(87.5, 95.1, 75, 88.3, 83, 92.7, 88.5, 96, 118.9, 62.5) > hist(dbh.edge) > mean(dbh.edge) [1] 88.75 > sd(dbh.edge) [1] 14.64591 > hist(dbh.edge, xlim=c(0,150)) > x.edge <- seq(0,150,1) # Generate sequence of x=values from 0 to 150 > # Generate probabilities for normal distribution with mean, SD > # from edge sample. Scale to histogram (sample size, interval width) > y.edge <- dnorm(x.edge, mean=mean(dbh.edge), sd=sd(dbh.edge))*10*10 > # multiply by n=10 and multiply by interval width=10 > # Superimpose normal curve on histogram > lines(x.edge, y.edge) > # ******************************************************* > # To save figures as graphics files, e.g., to submit with > # homework assignments, right-click on graphics window > # and select preferred format. > # More elegant approach is to use R's graphics device commands > # below, but these do not appear to work in AH 05 computers. > # To save in JPEG format, with file named "edge.jpg": > jpeg(file="edge.jpg") > hist(dbh.edge, xlim=c(50, 120), xlab="DBH, edge trees (m)", main="") > dev.off() > # To save in PDF format, with file named "edge.pdf": > pdf(file="edge.pdf") > hist(dbh.edge, xlim=c(50, 120), xlab="DBH, edge trees (m)", main="") > dev.off() > # To save in PostScript format, with file named "edge.ps": > postscript(file="edge.ps") > hist(dbh.edge, xlim=c(50, 120), xlab="DBH, edge trees (m)", main="") > dev.off() > # Descriptive statistics: > mean(dbh.int) [1] 89.03 > median(dbh.edge) [1] 88.4 > median(dbh.int) [1] 95.3 > sd(dbh.edge) [1] 14.64591 R version 2.11.1 (2010-05-31) Copyright (C) 2010 The R Foundation for Statistical Computing ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # this is a comment > dbh.edge <- c(87.5, 95.1, 75, 88.3, 83, 92.7, 88.5, 96, 118.9, 62.5) > dbh.edge [1] 87.5 95.1 75.0 88.3 83.0 92.7 88.5 96.0 118.9 62.5 > length(dbh.edge) [1] 10 > dbh.int <- c(115.2, 80, 91.6, 63, 112, 107, 52.1, 102.4, 99, 68.0) > dbh.int [1] 115.2 80.0 91.6 63.0 112.0 107.0 52.1 102.4 99.0 68.0 > length(dbh.int) [1] 10 > dbh.int <- dbh.int[1:9] > dbh.int [1] 115.2 80.0 91.6 63.0 112.0 107.0 52.1 102.4 99.0 > dbh.int <- c(dbh.int, 68.0) > dbh.int [1] 115.2 80.0 91.6 63.0 112.0 107.0 52.1 102.4 99.0 68.0 > dbh.int[10] <- 2 > dbh.int [1] 115.2 80.0 91.6 63.0 112.0 107.0 52.1 102.4 99.0 2.0 > dbh.int[10] <- 68.0 > dbh.int [1] 115.2 80.0 91.6 63.0 112.0 107.0 52.1 102.4 99.0 68.0 > dbh.edge [1] 87.5 95.1 75.0 88.3 83.0 92.7 88.5 96.0 118.9 62.5 > dbh.int [1] 115.2 80.0 91.6 63.0 112.0 107.0 52.1 102.4 99.0 68.0 > dbh.all <- c(dbh.edge, dbh.int) > dbh.all [1] 87.5 95.1 75.0 88.3 83.0 92.7 88.5 96.0 118.9 62.5 115.2 80.0 [13] 91.6 63.0 112.0 107.0 52.1 102.4 99.0 68.0 > hist(dbh.all) > hist(dbh.edge) > hist(dbh.int) > par(mfrow = c(3,1)) > hist(dbh.all) > hist(dbh.edge) > hist(dbh.int) > hist(dbh.all, xlim=c(50, 120)) > hist(dbh.edge, xlim=c(50, 120)) > hist(dbh.int, xlim=c(50, 120)) > hist(dbh.all, xlim=c(50, 120), xlab="DBH, all trees (m)", + main="Histogram of tree diameters") > hist(dbh.edge, xlim=c(50, 120), xlab="DBH, edge trees (m)", main="") > hist(dbh.int, xlim=c(50, 120), xlab="DBH, interior trees (m)", main="") > x.20 <- seq(from=40, to=120, by=20) > x.20 [1] 40 60 80 100 120 > hist(dbh.all, xlim=c(40, 120), xlab="DBH, all trees (m)", main="", breaks=x.20) > mean(dbh.edge) [1] 88.75 > mean(dbh.int) [1] 89.03 > median(dbh.edge) [1] 88.4 > median(dbh.int) [1] 95.3 > par(mfrow = c(1,1)) > hist(dbh.edge) > sd(dbh.edge) [1] 14.64591 > jpeg(file="dbh.tree.jpg") Error in jpeg(file = "dbh.tree.jpg") : unable to start device In addition: Warning messages: 1: In jpeg(file = "dbh.tree.jpg") : Unable to open file 'dbh.tree.jpg' for writing 2: In jpeg(file = "dbh.tree.jpg") : opening device failed > pdf(file="dbh.pdf") Error in pdf(file = "dbh.pdf") : unable to start device pdf In addition: Warning message: In pdf(file = "dbh.pdf") : cannot open 'pdf' file argument 'dbh.pdf' >