Statistics with R
Student Resources
Chapter 1: Introduction and R Instructions
1. The eight-day forecast of high-and-low temperatures (in degrees Fahrenheit) for New York City for the period from June 8 to June 15, 2017 is: 68 and 56; 82 and 64; 84 and 69; 93 and 70; 91 and 71; 90 and 70; 84 and 64; and 78 and 69. Create a data frame consisting of the two variables (name them “high” and “low,” respectively). Name the data frame “temps.” Use R to find the average difference between the high and low temperatures across the eight days. Hint: (1) find the difference by subtracting the low from high temperatures, and assign the difference to an object named “difference,” and (2) use the mean() function to find the average difference.
- 18.925
- 17.125 X
- 15.250
- 16.655
Solution:
> high <- c(68, 82, 84, 93, 91, 90, 84, 78)
> low <- c(56, 64, 69, 70, 71, 70, 64, 69)
> temps <- data.frame(High = high, Low = low)
> difference <- temps$High - temps$Low
> mean(difference)
[1] 17.125
2. What are the maximum high and low temperatures? Use the max() function.
- 89 and 69
- 91 and 70
- 93 and 71 X
- 94 and 72
Solution:
> max(temps$High)
[1] 93
> max(temps$Low)
[1] 71
3. What are the minimum high and low temperatures? Hint: use the min() function.
- 72 and 59
- 64 and 57
- 70 and 54
- 68 and 56 X
Solution:
> min(temps$High)
[1] 68
> min(temps$Low)
[1] 56
4. What are the mean high and low temperatures? Hint: use the mean() function.
- 80.665 and 64.775
- 79.780 and 65.970
- 83.75 and 66.625 X
- 85.275 and 68.710
Solution:
> mean(temps$High)
[1] 83.75
> mean(temps$Low)
[1] 66.625
5. What are the median high and low temperatures? Hint: use the median() function.
- 84 and 69 X
- 79 and 66
- 81 and 67
- 86 and 71
Solution:
> median(temps$High)
[1] 84
> median(temps$Low)
[1] 69
6. How many observations are in “temps”? Hint: use the nrow() function.
- 7
- 9
- 6
- 8 X
Solution:
> nrow(temps)
[1] 8
7. How many columns are in “temps”? Hint: use the ncol() function.
- 4
- 3
- 2 X
- 1
Solution:
> ncol(temps)
[1] 2
8. If “temps” has variable names, what are they? Hint: use the names() function.
- “X” and “Y”
- “High” and “Low” X
- “A” and “B”
- “Var1” and “Var2”
Solution:
> names(temps)
[1] "High" "Low"
9. What are the first observations of “High” and “Low”? Hint: use head(,1) function.
- 68 and 56 X
- 78 and 69
- 84 and 69
- 90 and 70
Solution:
> head(temps$High, 1)
[1] 68
> head(temps$Low, 1)
[1] 56
10. What are the final observations of “High” and “Low”? Hint: use tail(,1) function.
- 68 and 56
- 78 and 69 X
- 84 and 69
- 90 and 70
Solution:
> tail(temps$High, 1)
[1] 78
> tail(temps$Low, 1)
[1] 69