---
title: 'Lab 2: Problem Solving'
format:
  html:
    embed-resources: true
editor: source
knitr:
  opts_chunk:
    message: false
---

###### Question 1
In the Console, enter `5 + 2` and press <kbd>Enter</kbd> to run the code.

###### Question 2
In the Console, type in `result <- 5 + 2` and press <kbd>Enter</kbd> to run the code.

Then, type in `applesauce <- 10 * 3` and press <kbd>Enter</kbd> to run the code.

###### Question 3
Type `result` into the Console and press <kbd>Enter</kbd> to print it. Do the same with `applesauce`.

###### Question 4
In the Console, enter `result + applesauce` and press <kbd>Enter</kbd> to run the code.

###### Question 5
In the Console, enter `result <- 300` and press <kbd>Enter</kbd> to run the code. Then, try `result + applesauce` again. What has changed? Why?

###### Question 6
 
The `sum()` function adds up all of its arguments, and outputs the grand total. In the Console, enter `sum(5, 2)` and pressing <kbd>Enter</kbd>. What were the arguments you passed to the `sum()` function?

###### Question 7
In your template file, locate the code chunk that looks like this

```{r unnamed-chunk-14}
#| label: result_plus_applesauce
#| document: show

result <- 5 + 2
applesauce <- 10 * 3

```
Add `result + applesauce` to the blank line at the end of the code chunk. Click the green triangle "play" button in the upper right hand corner of the code chunk to execute the code in the chunk.

###### Question 8
Edit the code below to correct the issues causing the "invalid 'type' (character) of argument" error, and then press the green "Play" button to run the corrected code.

```{r unnamed-chunk-21}
#| document: template
#| echo: true
#| eval: false

sum("3", "4")
```
###### Question 9
Imagine you recorded the temperature outside of your home at 11:00 am each day for ten day, and observed the following sequence of temperatures

```
68, 70, 78, 75, 69, 80, 66, 66, 79
```

First, insert a new code chunk into your Quarto document.
Inside the code chunk, write code that will create a vector object named `daily_temps` to store these values.

Lastly, write code inside this code chunk that uses the `sort()` function to arrange these temperatures in descending order (i.e., from highest to lowest). Be sure to read the help page to find out how to change the sort order from ascending to descending!

When you think you've got it, run the code by pressing the green "play" button on your code chunk. You might not get it right the first time, and that's OK! If you get an error message, reading your error message carefully, and practice your troubleshooting skills!

###### Question 10
Use [this cheatsheet](https://iqss.github.io/dss-workshops/R/Rintro/base-r-cheat-sheet.pdf) to find an which R function will allow you to a generate a complex sequence of numbers. Then, use this function to generate a create a vector object named `first_50_even` representing the first 50 even numbers. Your code should print out this object after you create it, so you can double-check that it matches the sequence above.

###### Question 11
Using a comment, add a properly formatted citation for [this Stack Overflow post](https://stackoverflow.com/questions/54017285/difference-between-sort-rank-and-order) to the code chunk above.

###### Question 12
Use `getwd()` to report the path to R's current working directory

###### Question 13

1.  Insert a new code chunk into your Quarto document, and copy the code template below into your new code chunk
    
    
2.  In the console, use `getwd()` to print out the path to your working directory.
3.  Copy the output from `getwd()`, including the quotation marks
4.  Replace the blank inside the parenthesis of the `readLines()` function with the path you copied
5.  Add `/paths.txt` to the end of the path, inside the quotation marks
6.  Press the green triangle "play" button on your code chunk. If you've specified the path correctly, R should print out what you wrote in that text file!

If you get an error message that ends with "No such file or directory" or "cannot open the connection", double check the location you saved the paths.txt file, and double check that you specified the path correctly.


###### Question 14
Use the `readLines()` function to print out what you stored in the [paths.txt]{.filename} file, but this time, use a *relative path* to the `paths.txt` file to locate it. Remember, relative paths mean "look for this file starting from your current location".

Remember, to use a relative path you have to know

1. The folder where your file is stored, and the precise file name
2. R's current working directory
3. How to travel from the current working directory to the folder where your file is stored

**If** your current working directory is same folder where your file is stored, you can omit the names of any folders, and just put in the name of the file, like this: `readLines("paths.txt")` 

