Chapter 1: Introduction and R Instructions

1. Using R, create a vector consisting of the following elements: 81, 17, 7, 55, 2, 98, 71, 47, 19, 8, 3, 10, 28, 65, 80. Name it E1_1.

Answer:

E1_1 <- c(81, 17, 7, 55, 2, 98, 71, 47, 19, 8, 3, 10, 28, 65, 80)

a. How many data values are in E1_1? Use length().

Answer:

length(E1_1)
## [1] 15

b. What is the mean of E1_1?

Answer:

mean(E1_1)
## [1] 39.4

c. What is the median of E1_1? Use median()?

Answer:

median(E1_1)
## [1] 28

d. Use the functions min() and max() to nd the minimum and maximum values.

Answer:

min(E1_1)
## [1] 2

max(E1_1)
## [1] 98

e. What is the sum of the values in E1_1? Use the function sum().

Answer:

sum(E1_1)
## [1] 591

2. Use the functions sum() and length() to nd the mean of E1_1.

Answer:

sum(E1_1)/length(E1_1)
## [1] 39.4

3. Using the built-in data set LakeHuron, please answer the following questions. (For a description of what LakeHuron is and where it can be found, see Exercise 3 of Chapter 1.)

a. What are the first five values in LakeHuron?

Answer:

head(LakeHuron,5)
## [1] 580.38 581.86 580.97 580.80 579.79

b. How many data values are in LakeHuron?

Answer:

length(LakeHuron)
## [1] 98

c. What is the lowest level (in feet) of LakeHuron during the 1875-1972 period?

Answer:

min(LakeHuron)
## [1] 575.96

d. What is the highest level (in feet) of LakeHuron?

Answer:

max(LakeHuron)
## [1] 581.86

e. What is the mean level?

Answer:

mean(LakeHuron)
## [1] 579.0041

f. What is the median level?

Answer:

median(LakeHuron)
## [1] 579.12

g. What are the last 4 values in the LakeHuron data set? Use function tail(,4).

Answer:

tail(LakeHuron,4)

## [1] 579.74 579.31 579.89 579.96 

4. Suppose we interview 5 individuals who are registering to vote in the 2016 US Presidential election, and learn the following about them in terms of their Age (years) and Income (annual): voter 1 is 25 years of age and reports an annual income of $24,000; voter 2 is 37 years with an income of $42,000; voter 3 is 45 years with an income of $39,000; voter 4 is 57 years with income of $77,000; and voter 5 is 65 years with income $84,000. Use R to create a data frame consisting of these 5 individuals and 2 variables. Name the variables Age and Income and the data frame E1_ 2.

Answer:

#Comment1. create the variable for Age

age <- c(25, 37, 45, 57, 65)

#Comment2. create the variable for Income

income <- c(24000, 42000, 39000, 77000, 84000)

#Comment3. create the data frame E1_2

E1_2 <- data.frame(Age = age, Income = income)

#Comment4. report the contents of the data frame E1_2

E1_2

##      Age   Income
##  1   25     24000
##  2   37    42000
##  3   45    39000
##  4   57    77000
##  5   65    84000

5. Use the function summary() to find the minimum, maximum, mean, median, 1st and 3rd quartiles of both variables Age and Income of the data frame E1_2.

Answer:

#Comment1. the summary statistics for Age

summary(E1_2$Age)

##        Min. 1st Qu. Median   Mean 3rd Qu. Max.
##        25.0     37.0     45.0     45.8     57.0     65.0

#Comment2. the summary statistics for Income

summary(E1_2$Income)

##        Min. 1st Qu. Median   Mean 3rd Qu.   Max.

##        24000 39000   42000  53200 77000   84000