Part 1
1-4+2-3+5-7+11
## [1] 5
4/9*(96-31)
## [1] 28.88889
21000/(1-(1+0.035)^-36)
## [1] 29570.5
25000*(1+(0.043/12))^(12*25)
## [1] 73109.21
(-1/3)+log(24)+exp(3)*sqrt((3/5)+3*5^-2)
## [1] 19.88786
Part 2
- Create and store a vector of values starting at 5 and ending at 73,
with the values increasing in increments of 4.
vector<- c(seq(from = 4, to = 74, by = 4))
- Add 2 to all of the values in the vector from problem 1 and store
the resulting vector. Do this by referring to the vector you made in
problem 1. Do not print anything.
vector1 <- vector + 2
- Remove the seventh value from the vector in problem 2 and store the
new vector. Print the result.
vector2 <- vector1[-7]
vector2
## [1] 6 10 14 18 22 26 34 38 42 46 50 54 58 62 66 70 74
4.Add the values 25 and 31 to the end of the vector in problem 3 and
store the new vector. Print the result.
k1 <- c(vector2 , 25 , 31)
k1
## [1] 6 10 14 18 22 26 34 38 42 46 50 54 58 62 66 70 74 25 31
- Suppose the fourth value in the vector from problem 4 was supposed
to be a 22. Make the appropriate change in the vector and print the
resulting vector.
k1[4] <- 22
k1
## [1] 6 10 14 22 22 26 34 38 42 46 50 54 58 62 66 70 74 25 31
- Sort the vector from problem 5 so that all of the values are in
increasing order. Then store the new vector. Do not print anything
here.
y <- sort(k1, decreasing = FALSE)
- Using an R function, find the length of the vector in problem
6.
length(y)
## [1] 19
- Create a new vector by repeating the number zero 40 times. Print the
result. Do not store this vector.
z <- c(0)
rep(z, times = 40)
## [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## [39] 0 0
- Recreate the following vector using the rep() function: 5, 5, 5, 1,
1, 1, 4, 4, 4, 2, 2, 2, 1, 1, 1. Also store it.
l <- c(5,1,4,2,1)
l1 <- rep(l, each =3)
l1
## [1] 5 5 5 1 1 1 4 4 4 2 2 2 1 1 1
- Combine the vectors from problems 5 and 9, and then print the
result. You do not need to store anything here.
c(rep(z, times = 40),l1)
## [1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
## [39] 0 0 5 5 5 1 1 1 4 4 4 2 2 2 1 1 1
Part 3
- The following are the final high school GPAs of a group of college
students: 3.63, 3.81, 3.96, 4.00, 3.12, 2.86, 3.77, 2.53, 3.26, and
2.29. Create a vector that stores these values in the order specified.
Choose a variable name that’s descriptive but relatively short.
hsGPA <- c(3.63 , 3.81 , 3.96 , 4.00 , 3.12 , 2.86, 3.77 , 2.53 , 3.26 , 2.29)
- The following are the SAT quantitative scores for the same group of
students: 640, 660, 790, 770, 580, 560, 670, 440, 650, and 470,
respectively. Create another vector that stores these SAT scores in the
order provided.
SAT <- c(640, 660 , 790, 770, 580, 560, 670, 440, 650, 470)
- Suppose I made a mistake when recording the SAT quantitative score
for the fifth student, whose actually score was a 680. Make this change
using the vector you created in problem 2 and then print the resulting
vector.
SAT[5] <- 680
SAT
## [1] 640 660 790 770 680 560 670 440 650 470
Part 4
According to the International Monetary Fund (IMF), the countries
with the ten highest gross domestic products (GDPs) in 2018, from
highest to lowest, were the United States, China, Japan, Germany, the
United Kingdom, France, India, Italy, Brazil, and Canada. Their GDPs (in
millions of US dollars) were 20,513,000, 13,457,267, 5,070,626,
4,029,140, 2,808,899, 2,794,696, 2,689,992, 2,086,911, 1,909,386, and
1,733,706, respectively.
- Create a variable that consists of the GDPs specified above and then
print it. Use an appropriate name when storing, and be careful with
commas!
HLGPD <- c(20513000 , 13457267 , 5070626 , 4029140 , 2808899 , 2794696 , 2689992 , 2086911 , 1909386 , 1733706)
- Create a new variable that measures the GDPs in trillions of US
dollars by converting the GDPs you stored in problem 1. You must use R
to convert the values for you (recall: R is a vectorized language). Then
print the resulting vector.
tHLGPD <- HLGPD/10^13
tHLGPD
## [1] 2.051300e-06 1.345727e-06 5.070626e-07 4.029140e-07 2.808899e-07
## [6] 2.794696e-07 2.689992e-07 2.086911e-07 1.909386e-07 1.733706e-07
- What were Italy’s and Brazil’s GDPs in 2018? Use one of the vectors
you already created and subset it to print these values. You must do
this in one line of code! Then type your answers (in a full sentence)
above the R code chunk to practice typing text outside of R code
chunks.
Italy’s GPD is the eighth in the top 10 according to the IMF being
$2,086,911.
Brazil is the 9th highest in GPD according to the IMF being
$1,909,386.
HLGPD[8:9]
## [1] 2086911 1909386
- What was the mean GDP (in 2018) of the ten countries listed above?
Calculate this value and then type a full sentence that answers the
question. This is meant to give you more practice typing text outside of
R code chunks.
The mean GPD is $5,709,362, this function adds all 10 GPDs and then
divides that number by ten to find the mean.
mean(HLGPD)
## [1] 5709362
- What was the total (combined) GDP (in 2018) of the ten countries
listed above? Calculate this value and then type a full sentence that
answers the question. This is meant to give you even more practice
typing text outside of R code chunks.
sum(HLGPD)
## [1] 57093623
Part 5
- Create and store a vector that consists of the following movie
lengths (in minutes): 94, 109, 110, 123, 125, 108, 92, 106, 84, 119,
110, and 140.
lmovies <- c(94, 109, 110, 123, 125, 108, 92, 106, 84, 119, 110, 140)
- How many movie lengths are in the vector from problem 1? Use an
appropriate R function to determine this, and then type your answer
above your code (outside of the R code chunk).
There are 12 movies in the lmovies vector.
length(lmovies)
## [1] 12
- For each movie, determine if the movie is at least 90 minutes long.
Your result should be a vector of logicals.
lmovies >= 90
## [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE TRUE TRUE TRUE
- Determine if any movies are over two hours long. Your result should
involve a logical.
any(lmovies > 120)
## [1] TRUE
- Determine if all of the movies are shorter than 90 minutes. Your
result should involve a logical.
all(lmovies > 90)
## [1] FALSE
- Print the movie lengths that are between 90 and 120 minutes,
inclusive.
lmovies >= 90 & lmovies <= 120
## [1] TRUE TRUE TRUE FALSE FALSE TRUE TRUE TRUE FALSE TRUE TRUE FALSE
(lmovies[c(T,T,T,F,F,T,T,T,F,T,F)])
## [1] 94 109 110 108 92 106 119 140
- Print the indices of the movie lengths that are between 95 and 115
minutes, inclusive.
lmovies >= 95 & lmovies <=115
## [1] FALSE TRUE TRUE FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE
subsetting the values that are TRUE
(lmovies[c(F, T, T, F, F, T, F, T, F, F, T, F)])
## [1] 109 110 108 106 110
- Print the movie lengths that are at least 90 minutes but not equal
to 110 minutes.
lmovies >= 90 & lmovies != 110
## [1] TRUE TRUE FALSE TRUE TRUE TRUE TRUE TRUE FALSE TRUE FALSE TRUE
(lmovies[c(T,T,F,T,T,T,T,T,F,T,F,T)])
## [1] 94 109 123 125 108 92 106 119 140
- Print the movie lengths that are either under 90 minutes or over 120
minutes.
lmovies < 90 | lmovies > 120
## [1] FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
(lmovies[c(F,F,F,T,T,F,F,F,T,F,F,T)])
## [1] 123 125 84 140
- Print the indices of the movie lengths that are exactly either 123
or 110 minutes.
Use the which function to figure out what numbers in the vector are
either 123 or 110.
which(lmovies == 123 | lmovies == 110)
## [1] 3 4 11