PLEASE COPY THIS FILE TO YOUR BEFORE EDITING.

Molly Gutilla R-Journal

GEOG 5023: Quantitative Methods In Geography

This is an R Markdown document. Markdown is a simple formatting syntax for authoring web pages (click the MD toolbar button for help on Markdown).

When you click the Knit HTML button a web page will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist    
##  Min.   : 4.0   Min.   :  2  
##  1st Qu.:12.0   1st Qu.: 26  
##  Median :15.0   Median : 36  
##  Mean   :15.4   Mean   : 43  
##  3rd Qu.:19.0   3rd Qu.: 56  
##  Max.   :25.0   Max.   :120

You can also embed plots, for example:

plot(cars)

plot of chunk unnamed-chunk-2

Your R-Journal should be a list of function or techniques that you've used to complete labs and in class assignments. It will be useful to include worked examples in code. I have inserted an entry for a simple function (mean()).

Once you get the hand of markdown you should delete this line and all preceeding text.

mean(someData):

This function computes the mean of a R object. It computes:

\[ \bar{x} = \frac{\sum_{i=1}^N x_i}{N} \]

The equation above is an example of latex math, a widely use, and convient way to write equations.

An example of using the function mean():

## Create a small dataset to demonstrate the use of mean()
someData <- c(1, 3, 5, 7, 9)

## Compute the mean of the object someData
mean(someData)
## [1] 5

In-Class Intro & Homework 1 Concepts

General Notes & Rules

# “<-” is called assignment operator # using “<-” assigns a value to a variable

Read Tables and .csv Files

CO2 <- read.csv("C:/Users/Hallie/Dropbox/GEOG 5023 - Spring 2013/Data/CO2.csv", 
    sep = ",", header = T)
## Warning: cannot open file 'C:/Users/Hallie/Dropbox/GEOG 5023 - Spring
## 2013/Data/CO2.csv': No such file or directory
## Error: cannot open the connection

names(CO2)
## [1] "Plant"     "Type"      "Treatment" "conc"      "uptake"

Selecting Parts of Datasets

CO2$conc  # select a column
##  [1]   95  175  250  350  500  675 1000   95  175  250  350  500  675 1000
## [15]   95  175  250  350  500  675 1000   95  175  250  350  500  675 1000
## [29]   95  175  250  350  500  675 1000   95  175  250  350  500  675 1000
## [43]   95  175  250  350  500  675 1000   95  175  250  350  500  675 1000
## [57]   95  175  250  350  500  675 1000   95  175  250  350  500  675 1000
## [71]   95  175  250  350  500  675 1000   95  175  250  350  500  675 1000
CO2$Type
##  [1] Quebec      Quebec      Quebec      Quebec      Quebec     
##  [6] Quebec      Quebec      Quebec      Quebec      Quebec     
## [11] Quebec      Quebec      Quebec      Quebec      Quebec     
## [16] Quebec      Quebec      Quebec      Quebec      Quebec     
## [21] Quebec      Quebec      Quebec      Quebec      Quebec     
## [26] Quebec      Quebec      Quebec      Quebec      Quebec     
## [31] Quebec      Quebec      Quebec      Quebec      Quebec     
## [36] Quebec      Quebec      Quebec      Quebec      Quebec     
## [41] Quebec      Quebec      Mississippi Mississippi Mississippi
## [46] Mississippi Mississippi Mississippi Mississippi Mississippi
## [51] Mississippi Mississippi Mississippi Mississippi Mississippi
## [56] Mississippi Mississippi Mississippi Mississippi Mississippi
## [61] Mississippi Mississippi Mississippi Mississippi Mississippi
## [66] Mississippi Mississippi Mississippi Mississippi Mississippi
## [71] Mississippi Mississippi Mississippi Mississippi Mississippi
## [76] Mississippi Mississippi Mississippi Mississippi Mississippi
## [81] Mississippi Mississippi Mississippi Mississippi
## Levels: Quebec Mississippi
CO2[CO2$Type == "Quebec", ]
##    Plant   Type  Treatment conc uptake
## 1    Qn1 Quebec nonchilled   95   16.0
## 2    Qn1 Quebec nonchilled  175   30.4
## 3    Qn1 Quebec nonchilled  250   34.8
## 4    Qn1 Quebec nonchilled  350   37.2
## 5    Qn1 Quebec nonchilled  500   35.3
## 6    Qn1 Quebec nonchilled  675   39.2
## 7    Qn1 Quebec nonchilled 1000   39.7
## 8    Qn2 Quebec nonchilled   95   13.6
## 9    Qn2 Quebec nonchilled  175   27.3
## 10   Qn2 Quebec nonchilled  250   37.1
## 11   Qn2 Quebec nonchilled  350   41.8
## 12   Qn2 Quebec nonchilled  500   40.6
## 13   Qn2 Quebec nonchilled  675   41.4
## 14   Qn2 Quebec nonchilled 1000   44.3
## 15   Qn3 Quebec nonchilled   95   16.2
## 16   Qn3 Quebec nonchilled  175   32.4
## 17   Qn3 Quebec nonchilled  250   40.3
## 18   Qn3 Quebec nonchilled  350   42.1
## 19   Qn3 Quebec nonchilled  500   42.9
## 20   Qn3 Quebec nonchilled  675   43.9
## 21   Qn3 Quebec nonchilled 1000   45.5
## 22   Qc1 Quebec    chilled   95   14.2
## 23   Qc1 Quebec    chilled  175   24.1
## 24   Qc1 Quebec    chilled  250   30.3
## 25   Qc1 Quebec    chilled  350   34.6
## 26   Qc1 Quebec    chilled  500   32.5
## 27   Qc1 Quebec    chilled  675   35.4
## 28   Qc1 Quebec    chilled 1000   38.7
## 29   Qc2 Quebec    chilled   95    9.3
## 30   Qc2 Quebec    chilled  175   27.3
## 31   Qc2 Quebec    chilled  250   35.0
## 32   Qc2 Quebec    chilled  350   38.8
## 33   Qc2 Quebec    chilled  500   38.6
## 34   Qc2 Quebec    chilled  675   37.5
## 35   Qc2 Quebec    chilled 1000   42.4
## 36   Qc3 Quebec    chilled   95   15.1
## 37   Qc3 Quebec    chilled  175   21.0
## 38   Qc3 Quebec    chilled  250   38.1
## 39   Qc3 Quebec    chilled  350   34.0
## 40   Qc3 Quebec    chilled  500   38.9
## 41   Qc3 Quebec    chilled  675   39.6
## 42   Qc3 Quebec    chilled 1000   41.4
CO2.Q <- CO2[CO2$Type == "Quebec", ]  # creates new dataset

Creating New Datasets

CO2$concat[CO2$conc <= 200] <- 1  # can create a new variable  by assignment
CO2$concat[CO2$conc > 200 & CO2$conc <= 400] <- 2  # can create a new variable by assignment
CO2$new <- CO2$conc * CO2$uptake  # can create a new variable by arithmetic
CO2$new <- CO2$uptake/1000  # can create a new variable by arithmetic

Histogram

hist(CO2$uptake, col = "blue", breaks = seq(0, 60, by = 2), xlab = "Rate (umol/m^2 sec)", 
    main = "CO2 Uptake Rates")

plot of chunk unnamed-chunk-7

# parameter 'breaks' allows you to bin data -- number of categories to
# plot, breakpoints for each category # xlab: x axis label # ylab: y axis
# label # col: color

Boxplot

boxplot(CO2$uptake ~ CO2$Type, main = "CO2 Uptake Rates", ylab = "Rate (umol/m^2 sec)", 
    col = rainbow(2))  # can also plot by categories

plot of chunk unnamed-chunk-8

Scatter plot

plot(CO2$conc, CO2$uptake, xlab = "CO2 Concentration", ylab = "CO2 Uptake Rate", 
    pch = 2, col = "red")

plot of chunk unnamed-chunk-9

# Other Parameters: # ylim - range for y axis # xlim - range for x axis # main - provides title

Basic Line Graphics

# good for time series data # consider flow gauge data for 2 Thames tributaries

thames2 <- read.csv("C:/Users/Hallie/Dropbox/GEOG 5023 - Spring 2013/Data/thames.csv", 
    sep = ",", header = T)
## Warning: cannot open file 'C:/Users/Hallie/Dropbox/GEOG 5023 - Spring
## 2013/Data/thames.csv': No such file or directory
## Error: cannot open the connection
names(thames2)
## Error: object 'thames2' not found
t1 <- 1:nrow(thames2)
## Error: object 'thames2' not found
plot(t1, thames2$Feildes, xlab = "Time", ylab = "Flow Rate", main = "Flow Rate for Feildes")  #scatter plot
## Error: object 't1' not found
plot(t1, thames2$Feildes, xlab = "Time", ylab = "Flow Rate", main = "Flow Rate for Feildes", 
    type = "l", ylim = c(-1, 100))  # line plot of scatter plot basically
## Error: object 't1' not found
lines(t1, thames2$Redbridge, lty = 2, col = "red")  # add another line to the graph
## Error: object 't1' not found
legend(5, 8, c("Feildes", "Redbridge"), lty = c(1, 2), col = c("black", "red"))  # adding a legend
## Error: plot.new has not been called yet

Adding a legend

# legend(x, y, names, line types, colors) # x: x coordinate # y: y coordinate # names: names of series in column format # line type: corresponding line types # colors: corresponding colors

Paneling Graphics

# More than one graphic on a panel # Can partition garphics panel to yield a framework in which to panel our plots # example code: par(mfrow = c( nrow, ncol)) # nrow is number of rows ## ncol is number of columns

par(mfrow = c(2, 2))
hist(CO2$uptake, col = "blue", breaks = seq(0, 50, by = 5), xlab = "Rate (umol/m^2 sec)", 
    main = "CO2 Uptake Rates")
plot(CO2$conc, CO2$uptake, xlab = "CO2 Concentration", ylab = "CO2 Uptake Rate", 
    pch = 2, col = "red")
boxplot(CO2$uptake ~ CO2$Type, main = "CO2 Uptake Rates", ylab = "Rate (umol/m^2 sec)", 
    col = rainbow(2))
plot(t1, thames2$Feildes, xlab = "Time", ylab = "Flow Rate", main = "Flow Rate for Thames Tributaries", 
    type = "l")
## Error: object 't1' not found
lines(t1, thames2$Redbridge, lty = 2, col = "red")
## Error: object 't1' not found
legend(5, 8, c("Feildes", "Redbridge"), lty = c(1, 2), col = c("black", "red"))

plot of chunk unnamed-chunk-11