k_egg homework 1

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

print(rain <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22))
##  [1] 16 18 14 22 27 17 19 17 17 22 20 22
length(rain)
## [1] 12
### calculate deviations in rainfall from the mean
print(rainDeviations <- rain - mean(rain))
##  [1] -3.25 -1.25 -5.25  2.75  7.75 -2.25 -0.25 -2.25 -2.25  2.75  0.75
## [12]  2.75
### Square the deviations in rainfall (the S in RMS)
print(RainDeviationsSqrd <- rainDeviations^2)
##  [1] 10.5625  1.5625 27.5625  7.5625 60.0625  5.0625  0.0625  5.0625
##  [9]  5.0625  7.5625  0.5625  7.5625
### Calculate the mean of the squared deviations (the M in RMS)
print(meanRainDeviations <- mean(RainDeviationsSqrd))
## [1] 11.52
### Calculate the square root of the mean of the squared deviations (the R
### in RMS)
print(SD_rainfall <- sqrt(meanRainDeviations))
## [1] 3.394
### function to convert data (x) from inches to CM
in_to_cm <- function(x) {
    print((x)/0.3937)  #complete the return statement
}
in_to_cm(rain)
##  [1] 40.64 45.72 35.56 55.88 68.58 43.18 48.26 43.18 43.18 55.88 50.80
## [12] 55.88

####round 2: SD of rainrall calculated using a function

print(rain <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22))
##  [1] 16 18 14 22 27 17 19 17 17 22 20 22
### this took a while to figure out all the parenthesis...Note to self:
### it's easier to do when you work from the inside out.
SDfunction <- function(x) {
    print(sqrt(mean(((x) - mean(x))^2)))
}
SDfunction(rain)
## [1] 3.394