Next to “author:” in the section above, replace “TYPE YOUR NAME HERE” with your name.
For the following exercises, you will run code chunks and sometimes write or edit comments. The code is contained in “code chunks”, such as this:
To run a code chunk either click the green triangle on the right of the chunk or use a keyboard shortcut (Mac users: Command+Option+C or Command+Shift+Enter; Windows users: Ctrl+Alt+C or Ctrl+Shift+Enter).
Run the code chunk below to add 549 plus 727. (No need to edit comments.)
## [1] 1276
Let’s say you want to buy 5 apples and they each cost $2. Replace each comment with a description of the code below it. Then run the code chunk.
# create "apples" variable and assign 5 as value
apples <- 5
# create "price" variable and assign 2 as value
price <- 2
# calculate the total cost by multiplying the number of apples by the price per apple
totalCost <- apples*price
# print the total cost to the console
print(totalCost) ## [1] 10
Replace each comment with a description of the code below it. For the round() function, include a short description of the two arguments. Then run the code chunk.
## [1] 5
## [1] 5
## [1] 5
## [1] 5
## [1] 5.24
Replace each comment with a description of the code below it, then run the code chunk.
# create a vector containing monthly sales values for each of the 12 months
sales.by.month <- c(0, 100, 200, 50, 0, 0, 0, 0, 0, 0, 0, 0)
# display the monthly sales vector
sales.by.month## [1] 0 100 200 50 0 0 0 0 0 0 0 0
Replace each comment with a description of the code below it, then run the code chunk.
# extract the sales value for February (the second element) from the sales.by.month vector
february.sales <- sales.by.month[2]
# display the February sales value
february.sales## [1] 100
Replace each comment with a description of the code below it, then run the code chunk.
# create a vector containing the names of the 12 months
months <- c("January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December")
# display the months vector
months## [1] "January" "February" "March" "April" "May" "June"
## [7] "July" "August" "September" "October" "November" "December"
Replace each comment with a description of the code below it, then run the code chunk.
# extract the names of the summer months (June through August) from the months vector
summer <- months[6:8]
# display the summer months
summer## [1] "June" "July" "August"
Replace each comment with a description of the code below it, then run the code chunk.
## [1] TRUE
Replace each comment with a description of the code below it, then run the code chunk.
## [1] FALSE
Replace each comment with a description of the code below it, then run the code chunk.
## [1] FALSE TRUE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
Replace each comment with a description of the code below it, then run the code chunk.
# create a vector of month names where sales were greater than 0
months.sold <- months[sales.by.month > 0]
# display the months that had sales
months.sold## [1] "February" "March" "April" "May"
Replace each comment with a description of the code below it, then run the code chunk.
## [1] FALSE
Replace each comment with a description of the code below it, then run the code chunk.
# extract the names of the months where sales are exactly 25 OR sales are 100 or more
months[sales.by.month == 25 | sales.by.month >= 100]## [1] "February" "March" "May"
What was the most difficult/confusing part of this assignment? Type your answer below: indexing and altering vector elements.