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

###### Question

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


###### Question

::: question
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

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


###### Question

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


###### Question

::: question
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

 
::: question
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

::: question
In your template file, add `result + applesauce` to the end of the following code chunk. Click the green triangle button in the upper right hand corner of the code chunk to execute the code in the chunk. 
:::


```{r unnamed-chunk-13}
#| label: result_plus_applesauce
#| document: show
#| 
result <- 5 + 2
applesauce <- 10 * 3

```

###### Question

::: question
Fix the errors in the code below, and then press the green "Play" button to run the corrected code.
:::


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

sum("3", "4")
```

###### Question

::: question
Access the help pages for the function `sort()`. Write code below to sort the vector `a` in decreasing order.
:::


```{r unnamed-chunk-23}
#| document: show

a <- c(1, 6, 4, 4, 8, 2)
```

###### Question

::: question
Imagine we collected the temperature of our home each day for the past ten days (see code below). Let's say we wanted to find how each day ranked from coolest to hottest. In other words, we wanted to know: was day 1 the coolest day (rank 1) out of all 10 days? Or was day 1 only the 4th coolest (rank 4) day?

Use [this cheatsheet](https://iqss.github.io/dss-workshops/R/Rintro/base-r-cheat-sheet.pdf), to find our which R function will allow you to generate a ranking of each day's temperature. Then, search the help page for this function to determine how to handle ties (i.e., two or more days the same temperature) by randomly choosing one temperature to be "first". Make sure your code includes a short comment describing how ranking the temperatures is *different* than sorting the temperatures.
:::


```{r unnamed-chunk-26}
#| document: show

# Create a vector of temperatures
temps_to_rank <- c(68, 70, 78, 75, 69, 80, 66, 66, 79)

# Write code below to rank the days with random ties

# Replace this line with a comment to yourself (starting a line with a # in a code block or script file makes it a comment) describing how this function is different than sorting the data. 
```

###### Question

::: question
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

::: question
Use `getwd()` to report the path to R's current working directory
:::


###### Question

::: question
In the console, use `getwd()` again to get your working directory. Copy the output from that command (including the quotes!) whole path, and add `/paths.txt` to the end (inside the quotation marks).

Take the full path to your `paths.txt` file, and in the console, write the function `readLines(" PATH TO YOUR WORKING DIRECTORY /paths.txt")` and press <kbd>Enter</kbd>. R should read what you wrote in that text file!
:::


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

readLines("PUT YOUR ABSOLUTE PATH HERE")
# make sure there are only one set of quotes!

```

###### Question

::: question

Repeat the activity from the previous question, 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", and your current location *should* be your SDS 100 R project directory. from our project. So if you 

1) have saved the `paths.txt` in your SDS 100 R project directory, and 
2) you have entered your SDS 100 R project in RStudio

all you need to do is type in `readLines("paths.txt")` and press <kbd>Enter</kbd>.
:::


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

readLines("PUT YOUR RELATIVE PATH HERE")
# make sure there are only one set of quotes!

```

