A Quick Primer on The Format of Homework Assignments

This is an R Markdown document. We will use these documents to complete homework assignments for the class. What is really cool about R Markdown assignments is that we can combine written text with R code in the same document. You can output the RMarkdown as a PDF, HTML, or Microsoft Word file. It is a really nice tool for generating reports of your research! Markdown is a type-setting protocol - for this class you really do not need to know all the details of it, but as you read these homework assignments you may start to get a sense for how they work! In fact, the readings for this course are all generated using RMarkdown!

In RMarkdown, you can create code snippets by using “```{r name-of-code-snippet}” and then repeating three of the tick marks when you are done. On the top right corner of a code snippet, you will see a few symbols. The down-arrow (middle symbol) tells R to run every code snippet preceding the current one. The play button (right symbol) tells R to run the current code snippet. As well, if you click inside the code-snippet, you can hit Ctrl+Shift+Enter to run the code snippet as well. If you hit Ctrl+Enter, then R will run the line of code you are currently on. Here is an example code snippet:

print("This is a code snippet")
## [1] "This is a code snippet"
2*3
## [1] 6
summary(c(1,2,3,4,5))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##       1       2       3       3       4       5

If we hit the play button (or CTRL-SHIFT-ENTER), we see that R runs all of the code!

Completing Homework Assignments

So! The nice thing about RMarkdowns is we can write in both normal text (i.e., what you would write in a paper) or R Code. So, we will use these documents for homework. Some questions will ask you to write your own R code. Code snippet blocks will be provided telling you where to write the code. Some questions will ask for you to write a response to a question (e.g., “What are the assumptions that must be met to run a linear regression?”) You will complete the assignment by completing the assignment and then exporting the document as a PDF and submitting that file.

Homework Section 1: Arithmetic in R

In the following code snippet, please write the appropriate mathematical expressions after each comment. The first one is already completed, so you can see what is expected. Each answer should be written as one line. And don’t worry…this class is not going to make you solve a bunch of elementary school level word problems (after this code snippet that is lol):

## 1) Please add together three positive integers that add up to 23

## Answer:

2 + 14 + 7
## [1] 23
## 2) Multiply together two numbers to equal 168

## Answer:
167+1
## [1] 168
## 3) Raise 42 to the power of 5

## Answer:
42^5
## [1] 130691232
## 4) I got a 92, 90, and 84 on my exams. What was my average score (please write it out as an equation, not just the number)?
## Answer:
exam_score<- c(92,90,84)
meanscore<-mean(exam_score)
meanscore
## [1] 88.66667
## 5) Your weird investor neighbor tells you he has a great investment. He says he can triple your investment. You have $200 but decide to only invest $50 because you think he is kind of weird. But! It works and the next day he brings you your money! How much money do you have at the end (including what you didn't invest)? 

## Answer
Fund<-200
Invest<-50
Rate<-3
(Fund-Invest)+Invest*Rate
## [1] 300
## 6) When you were little, your uncle offered you a deal. You could either have $10 right now or he would start with a penny and double the amount every single day for one year (i.e., after one day it would be worth 2 cents, after 2 days it would be worth 4 cents, 3 days 8 cents,etc...). How much money would you have earned if you had waited a year on that penny? 

## Answer:
days<-365
option2<- 2^days/100
option2
## [1] 7.515336e+107

You should run the code if you haven’t to make sure the answers look right!

Section 2: Saving Values

It is important that we understand how to save values in R and to get a feeling for how to do so and work with saved values.

## This last week, I measured the high temperature at my house each day
## On Monday it was 38 degrees
## Tuesday it was 47, Wednesday 58, Thursday 30, Friday 16

## First, create five variables, one for each day of the week and store the temperature for that day. I'll get ya started:

Monday <- 38
Tuesday <-47
Wednesday <-58
Thursday <-30
Friday <- 16

## Next, I want you to calculate the average temperature, using the variables you created above, and save it in a variable called average_temp:
  
average_temp <- mean(Monday,Tuesday, Wednesday,Thursday, Friday)

Now, it can be useful to save values in a vector using the c() notation. In the next snippet I would like you to create a vector using the variables you created above. We are also going to use a couple of functions we learned about, the “summary()” function and the “mean()” function.

## First, I would like you to save the five temperatures you saved above into a vector called daily_highs. Please use the name of the variables to do so:

daily_highs <- c(Monday,Tuesday, Wednesday,Thursday, Friday)

## Second, I would like you to print out the 3rd element of the daily_highs vector
daily_highs[3]
## [1] 58
## Next I would like you to print out the 2nd through 4th elements of the vector. Please do this with only one line of code
daily_highs[c(2,3,4)]
## [1] 47 58 30
## Next I would like you to print out the 1st, 3rd, and 5th elements of the vector. Please do this with only one line of code
daily_highs[c(1,3,5)]
## [1] 38 58 16
## Next, I want you to run the summary() function, supplying daily_highs as the argument:
summary(daily_highs)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    16.0    30.0    38.0    37.8    47.0    58.0
## Finally, I want you to run the mean() function on daily highs:
mean(daily_highs)
## [1] 37.8

Section 3: Logical Operators and the If/Else Statement

We also learned about another kind of operator, the logical operator. We are familiar of many of these from math class. The answer to a logical operator is always either TRUE or FALSE.

## In the previous snippet, you used the mean() function to calculate the average high temperature for the week. You also calculated this earlier and saved it as "average_temp". Use the == operator to check and make sure that these two values are equal to each other. The computer should tell you TRUE as a response if done correctly.



## Now, I want you to ask the computer to check if the 2nd element of the array is less than the 4th element of the array. 


daily_highs[2]==daily_highs[4]
## [1] FALSE

Now, logicals may not seem super useful, but they allow us to do really important programming work. Specifically, there is a set of functions called if(), else if(), and else that we use logicals for. Basically, there might be situations where you want the computer to do one thing IF something is true but to do something else if it is not true.

## If, else if, and else statements are very powerful
## Below, I have laid out a frame for a sequence of if and else statements
## I want you to fill out the arguments for the statements correctly

if(mean(daily_highs)>32){
  
  print("The average daily high this week was above 32 degrees")
  
}else if(mean(daily_highs)==32){
  
  print("The average daily high this week was exactly 32 degrees")
  
}else{
  
  print("The average daily high this week was lower than 32 degrees")
  
}
## [1] "The average daily high this week was above 32 degrees"

While it might not be 100% clear now, these if-else statements allow us to tell the computer to do more complicated things. This is particularly true when we are cleaning or formatting our data. Especially when we combine the if() statement with a cool function call the for() loop.

Section 4: The For-Loop

Now, the for loop is a way of telling the computer to go through a sequence of numbers or elements of an array. Let me give you three samples to give you a sense of how this works. Please run the following code snippet:

## A for loop takes two arguments
## The first is a name for the variable you will feed into the for-loop: this is usually just a name that makes sense to you
## It is conventional in computer programming to use the variable name i
## The second part of the for-loop is a list of values to iterate through
## Each time through the for-loop, the variable (in this case i) will take a different value from the list
## In this example, we use 1:5 as the list, which is the same as writing c(1,2,3,4,5)

for(i in 1:5){ ## We would read this in English as "For each value 'i' in the list 1 through 5, do the following...
  
  ## What you want the for-loop to do is contained within the set of brackets { instructions     }
  ## In this for-loop, we will just print the value of i, which should be 1 then 2 then 3...
  
  print(i)
  
}
## [1] 1
## [1] 2
## [1] 3
## [1] 4
## [1] 5
## Now let's do a similar example, but this time, we are going to print each value in "daily_highs"
## Again we will have our variable be named i (but to be clear, you can give it another name if you so desire)
## Now, instead of 1:5, we are going to write 1:length(daily_highs). 
## length(daily_highs) does equal 5 in this case, but sometimes, you don't know the length of your vector, so we use this function

for(i in 1:length(daily_highs)){
  
  print(daily_highs[i])
  
}
## [1] 38
## [1] 47
## [1] 58
## [1] 30
## [1] 16
## One last example, we can also have the for-loop iterate through the objects in a vector
## In this case we typically don't use the letter i, but instead use a name that makes sense
## So I can tell the loop to iterate through the values of daily_highs and print them like so
## I will call my new variable daily_high because each loop we are taking ONE value from daily_highs

for(daily_high in daily_highs){
  
  print(daily_high)
  
}
## [1] 38
## [1] 47
## [1] 58
## [1] 30
## [1] 16

Now, this is a really useful tool! Sometimes we want to loop through a list or a data.frame in order to clean or manipulate our data. It can be very useful to pair together for-loops and if-statements. Let’s complete a couple of examples to see how this works:

## I want to write a little program that checks if the temperature each day reached 32 degrees
## Complete the following loop and if statements

for(day in daily_highs){
  
  if(day >= 32){
    
    print("The temperature today reached at least 32 degrees")
    
  }else{
    
    print("The temperature today did not reach 32 degrees")
    
  }
}
## [1] "The temperature today reached at least 32 degrees"
## [1] "The temperature today reached at least 32 degrees"
## [1] "The temperature today reached at least 32 degrees"
## [1] "The temperature today did not reach 32 degrees"
## [1] "The temperature today did not reach 32 degrees"
## Now, I want to create a vector that contains TRUE if the temperature was at least 32 degrees and FALSE if not
## Since we know the temperatures for the week were 38, 47, 58, 30, and 16, we know that the result should be T, T, T, F, F
## However, we want to use the loop we came up with above to do this

## First, we will create a new array, which I will call above_32:

above_32 <- c() ## This creates an empty vector

## A very cool trick is that if you want to add something, say X, to a vector you can use the c() function
## Let's say we want to add TRUE to above_32, we can write above_32 <- c(above_32, T). This will merge together the contents of above_32 with T
## You could also write above_32 <- c(T, above_32)...this would add T to the front of the vector

## So, now I want you to fill out the following loops to fill in above_32 correctly. 
above_32<-c()
for(day in daily_highs){
  
  if(day>=32){
    
    above_32 <- c(above_32, TRUE)
    
  }else{
    
    above_32 <- c(above_32, FALSE)
    
  }
}

Section 5: Data Frames

Now, we are going to merge some vectors together to create a data.frame. We are going to start by creating a 3-column data.frame: the first column will contain the day of the week; the second column will contain the daily high; and the third column will contain whether or not the high was above 32 degrees or not. Usefully, you have already created vectors that contain the information for columns 2 (daily_highs) and 3 (above_32). So, we will start by creating a vector of the days, then merge all three into a data.frame using the data.frame() function.

## First, let's create a vector called days, which contains the names of each day of the week corresponding to values in daily_highs and above_32, I'll get you started

days <- c("Monday","Tuesday", "Wednesday","Thursday", "Friday")

## Next, let's create a data.frame using the data.frame function. To do so, we will provide the three vectors as arguments to the data.frame function. It is common to name a data.frame either "data" or "df". However, you should choose names that make sense to you and that you think will make sense to someone else reading. For this assignment, please use the name "df". Please do this in the line below:

df <- data.frame(days, daily_highs, above_32)

Section 6: Writing A Function

Now, we have our data.frame. While it is nice to have temperature in Fahrenheit, my cousin happens to be a chemist and he can only understand temperature if it is provided to him in Kelvin. So, for his sake, we need to convert the temperatures into Kelvin. So, we are first going to write a function that converts a fahrenheit temperature into Kelvin temperature. For those of you not familiar, in order to convert Fahrenheit (Tf) into Kelvin (Tk): 1) you subtract 32 from your Fahrenheit temp (Tf - 32); 2) you multiply this new number by 5/9; and 3) you add 273.15. Or, this can be written as equation:

K = (F - 32)*(5/9) + 273.15

In the below snippet, I want you to complete the function f2k_converter().

## here we are going to name our function f2k_convert
## It will take one argument (temp_f)
## temp_f is the temperature in fahrenheit you want to convert to kelvin

f2k_convert <- function(temp_f){

  ## first we need to convert temp_f in kelvin
  ## we will save this value as temp_k
  ## Fill out the equation to make the conversion
  
  temp_k <- (temp_f - 32)*(5/9) + 273.15
  
  ## After you have generated the new value, we will use the return() function
  ## The return() function is our way of saying "This is the result!!"
  ## Complete the return function to finish the function
  
  return(temp_k )
  
}

Now that we have written the function, let’s just test it real quickly! You do not need to fill anything out, but you should confirm that the results are correct by running it:

## Let's convert 98.6 degrees F, which should equal 310.15 Kelvin
f2k_convert(98.6)
## [1] 310.15
## Let's convert 32 degrees F, which should equal 273.15 Kelvin
f2k_convert(32)
## [1] 273.15
## Let's convert 0 degrees F, which should equal 255.37 Kelvin
f2k_convert(0)
## [1] 255.3722
## Here's a cool thing too. You can also just provide all the numbers you want 
## to check as a vector. Then it will run the function separately for each, like so
f2k_convert(c(98.6, 32, 0))
## [1] 310.1500 273.1500 255.3722

Section 7: Creating New Column in Data.Frame

Now, for the final part of the assignment, let’s use our new function to create a new column in our data.frame df. To create a new column, we type the name of the data frame, followed by the “$”, and then the name of the new variable. I want you to create a new variable called “daily_highs_Kelvin”, and you will create it by converting each value in daily_highs into Kelvin using the function. You can do this with one line of code.

df$daily_highs_kalvin <- f2k_convert(daily_highs)