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.

  1. 18.925
  2. 17.125 X
  3. 15.250
  4. 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.

  1. 89 and 69
  1. 91 and 70
  1. 93 and 71 X
  1. 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.

  1. 72 and 59
  2. 64 and 57
  3. 70 and 54
  4. 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.

  1. 80.665 and 64.775
  2. 79.780 and 65.970
  3. 83.75 and 66.625 X
  4. 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.

  1. 84 and 69 X
  2. 79 and 66
  3. 81 and 67
  4. 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.

  1. 7
  2. 9
  3. 6
  4. 8 X

Solution:

> nrow(temps)

[1] 8

7.  How many columns are in “temps”? Hint: use the ncol() function.

  1. 4
  2. 3
  3. 2 X
  4. 1

Solution:

> ncol(temps)

[1] 2

8.  If “temps” has variable names, what are they? Hint: use the names() function.

  1. “X” and “Y”
  2. “High” and “Low” X
  3. “A” and “B”
  4. “Var1” and “Var2”

Solution:

> names(temps)

[1] "High" "Low"

9.  What are the first observations of “High” and “Low”? Hint: use head(,1) function.

  1. 68 and 56 X
  2. 78 and 69
  3. 84 and 69
  4. 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.

  1. 68 and 56
  2. 78 and 69 X
  3. 84 and 69
  4. 90 and 70

Solution:

> tail(temps$High, 1)

[1] 78

> tail(temps$Low, 1)

[1] 69