INSTRUCTIONS:

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:

# This is an example code chunk. 88

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).

HW2 EXERCISES:

Using R as a calculator

1. Addition

Run the code chunk below to add 549 plus 727. (No need to edit comments.)

# add 549 to 727
549 + 727
## [1] 1276

2. Multiplication

Run the code chunk below to multiply 88 by 4. (No need to edit comments.)

# Multiply 88 by 4
88 * 4
## [1] 352

Variables and Functions

3. Calculations with variables

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

4. Using functions: sqrt(), abs(), and round()

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.

# take square root of 25
sqrt(25) 
## [1] 5
# raise 25 to the power of 0.5
25 ^ 0.5 
## [1] 5
# take absolute value of -5
abs(-5) 
## [1] 5
# add 20 to the absolute value of -5, then take the square root of the result
sqrt(20 + abs(-5))
## [1] 5
# round the number 5.2357 to 2 decimal places
round(5.2357, 2)
## [1] 5.24

5. Numerical vectors

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

indexing and altering vector elements

6.

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

7.

Replace each comment with a description of the code below it, then run the code chunk.

# update the sales value for the fifth month to 25
sales.by.month[5] <- 25
# display the updated monthly sales vector
sales.by.month
##  [1]   0 100 200  50  25   0   0   0   0   0   0   0

8.

Replace each comment with a description of the code below it, then run the code chunk.

# return the number of elements in the sales.by.month vector
length(sales.by.month)
## [1] 12

Text data

9.

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"

10.

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"

11.

Replace each comment with a description of the code below it, then run the code chunk.

# extract the names of the winter months (November, December, and January) from the months vector
winter <- months[c(11,12,1)]
# display the winter months
winter
## [1] "November" "December" "January"

12. Using the nchar function

Replace each comment with a description of the code below it, then run the code chunk.

# count the number of characters in each month name
nchar (x = months)
##  [1] 7 8 5 5 3 4 4 6 9 7 8 8

True/False data & logical operations

13. Logical operators (Table 3.2)

Replace each comment with a description of the code below it, then run the code chunk.

# check if 50 is greater than or equal to 49
50 >= 49
## [1] TRUE

14. Logical operators (Table 3.3)

Replace each comment with a description of the code below it, then run the code chunk.

# check if either 1+1 equals 5 OR 1+1 equals 2
(1+1 == 5) & (1+1 == 2)
## [1] FALSE

15. Vectors of logicals (section 3.9.4)

Replace each comment with a description of the code below it, then run the code chunk.

# check which months have sales greater that 0
sales.by.month > 0
##  [1] FALSE  TRUE  TRUE  TRUE  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

16. Logical indexing (section 3.10.2)

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"

17. Logical operations (section 3.9.5)

Replace each comment with a description of the code below it, then run the code chunk.

# check if the string "statistics" is equal to the string "fun"
"statistics" == "fun"
## [1] FALSE

18. Logical indexing (section 3.10.2)

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"

19. Short answer question

What was the most difficult/confusing part of this assignment? Type your answer below: indexing and altering vector elements.