Type “1+3” into your new script window.
1 + 3
## [1] 4
Try using the functions sum() and length() to calculate the mean amount of rainfall, check your answer using the mean function.
## Creates an object 'rain' containing actual rainfall data for Boulder,
## CO in inches (2000-2011)
rain <- c(16, 18, 14, 22, 27, 17, 19, 17, 17, 22, 20, 22)
## Calculates the mean amount of rainfall using the funtions sum() and
## length()
sum(rain)/length(rain)
## [1] 19.25
## Compares the above answer using the mean function
mean(rain) == sum(rain)/length(rain)
## [1] TRUE
XW: The logical operator comparing the mean found from using the functions sum() and length() to the one fround from the mean function returns as TRUE, so they are equivalent.
We calculate the standard deviation as:
\[ s_N = \sqrt{\frac{1}{N} \sum_{i=1}^N (x_i - \overline{x})^2} \]
The standard deviation is the Root Mean Square (RMS) of the deviations from the mean. The above formula can be broken down into a series of simple steps:
1. Calculate the deviations from the mean (see above R code).
2. Square the deviations from the mean, save the squared deviations as a new R object (use the “<-” assignment operator).
3. Take the mean of these squared deviations. Again, save the results as an object.
4. Finally, take the square root of the result from the prior step.
Using the four steps above compute the standard deviation of the rainfall data. You have the correct answer if you get something close to 3.4.
## Calculates the deviations from the mean and saves them as an object
rainDeviations <- rain - mean(rain)
## Squares the deviations from the mean and saves them as a new object
rainDeviationsSqrd <- rainDeviations^2
## Takes the mean of the squared deviations and saves it as object
mean_rainDeviationsSqrd <- mean(rainDeviationsSqrd)
## Takes the square root of the mean of the squared deviations and saves
## it as an object
sqrt_mean_rainDeviationsSqrd <- sqrt(mean_rainDeviationsSqrd)
sqrt_mean_rainDeviationsSqrd
## [1] 3.394
XW: The above steps show the standard deviation of the rainfall data (which is calculated as the root mean square of the deviations from the mean) as 3.394235, which is pretty close to 3.4.
Now, complete the return statement below to create a function to convert inches to centimeters. Assume the input data are in inches and you want to return the same data converted to cm.
## Creates a function to convert data in inches to centimeters
in_to_cm <- function(someDataInInches) {
return((someDataInInches)/0.3937)
}
## Uses the in_to_cm funtion on the rainfall data
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
## Validates the answer with a conversion where no function is created
in_to_cm(rain) == rain/0.3937
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE
XW: The validation returns as true for all values of the rainfall data which were converted from inches to centimenters using the created function.
Write a function to compute the standard deviation, use the four steps outlined above. Do not use the internal sd() function to check your work because it uses \( \sqrt{\frac{1}{N} \sum_{i=1}^N (x_i - \overline{x})^2} \), note the N-1 in the denominator. Your function is correct if you find a standard deviation of around 3.39.
## Creates a function to compute the standard deviation using the four
## steps outlined in Task 3 above.
standDev <- function(someData) {
return(sqrt(mean(((someData) - mean(someData))^2)))
}
## Uses the standDev function to find the standard deviation of rainfall
## data
standDev(rain)
## [1] 3.394
XW: The answer checks out.