Homework 1 – Alex Crawford

GEOG 5023: Quantitative Methods In Geography

SS: Type “1+3” into your new script window

1 + 3  #AC: Addition
## [1] 4

Basic Data Characteristics

SS:'rain' contains actual rainfall data for Boulder, CO (2000-2011)

rain <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22)

AC: c() is the combine function; it can create an object from a list of data.

New functions:

SS: Try using the functions sum() and length() to clacluate the mean amount of rainfall, check your answer using the mean function.

sum(rain)/length(rain)
## [1] 19.25
sum(rain)/length(rain) == mean(rain)
## [1] TRUE

AC: It returns TRUE, so I have correctly calculated the mean.

Calculating Standard Deviation

New functions/operations:

rain - 1  #AC: Subtraction
##  [1] 15 17 13 21 26 16 18 16 16 21 19 21
rain^2  #AC: Powers
##  [1] 256 324 196 484 729 289 361 289 289 484 400 484
sqrt(rain)  #AC: Square Root Function
##  [1] 4.000 4.243 3.742 4.690 5.196 4.123 4.359 4.123 4.123 4.690 4.472
## [12] 4.690

SS: Using the four steps above compute the standard deviation of the rainfall data.
SS: You have the correct answer if you get something close to 3.4.

rain.dev <- rain - mean(rain)  #AC: Step 1: take deviations from mean
rain.dev.sq <- rain.dev^2  #AC:Step 2: square the deviations
rain.dev.sq.mean <- mean(rain.dev.sq)  #AC: Step 3: Take the mean of square deviations
rain.std.dev <- sqrt(rain.dev.sq.mean)  #AC: Step 4: Take the square root of the mean square deviations (hence RMS = root mean square).
rain.std.dev
## [1] 3.394

AC: It returns 3.394235

Creating Functions

SS: Now, complete the return statement below to create a function to convert inches to centimeters.

in_to_cm <- function(someDataInInches) {
    return(someDataInInches/0.3937)  #complete the return statement
}  #SS: someDataInInches is just a placeholder; AC: Use a name that will make sense for the placeholder
in_to_cm(1)
## [1] 2.54

AC: To test, I know that 1 should return 2.54 because 1in = 2.54cm. It checks out.

SS: Write a function to compute the standard deviation, use the four steps outlined above.

stdevp <- function(someData) {
    return(sqrt(mean((someData - mean(someData))^2)))
}
stdevp(rain)  #AC: Returns 3.394235; it checks out.
## [1] 3.394
stdevp(rain) == rain.std.dev  #AC: And this statement returns TRUE.
## [1] TRUE