Lab 2: Problem Solving

Question

In the Console, enter 5 + 2 and press Enter to run the code.

You should see the following output in your console:

[1] 7
Question

In the Console, type in result <- 5 + 2 and press Enter to run the code.

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

No new result should be displayed in your console, but you should see two new objects appearing in your Environment pane!

Question

Type result into the Console and press Enter to print it. Do the same with applesauce.

[1] 7
[1] 30
Question

In the Console, enter result + applesauce and press Enter to run the code.

You should see the following output in your console:

[1] 37
Question

In the Console, enter result <- 300 and press Enter to run the code. Then, try result + applesauce again. What has changed? Why?

You should see the following output in your console:

[1] 330
Question

The sum() function adds up all of its arguments, and outputs the grand total. In the Console, enter sum(5, 2) and pressing Enter. What were the arguments you passed to the sum() function?

There were two arguments: the number 5, and the number 2

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.

[1] 37
Question

Fix the errors in the code below, and then press the green “Play” button to run the corrected code.

Once you correct the code and run it, you should see the following output in your console:

[1] 7
Question

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

Once you sort the vector a, you should see the following output in your console:

[1] 8 6 4 4 2 1
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, 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.

# Ranking the temperatures only reports their 'finishing' place (i.e., 1st, 2nd, 3rd, etc.),
# but sorting actually re-arranges the temperatures according to rank.
[1] 3 5 7 6 4 9 2 1 8
Question

Using a comment, add a properly formatted citation for this Stack Overflow post to the code chunk above.

# Ranking the temperatures only reports their 'finishing' place (i.e., 1st, 2nd, 3rd, etc.),
# but sorting actually re-arranges the temperatures according to rank.
# Amit Gupta. 2019, Jan 3. Difference between sort(), rank(), and order(). Stack Overflow.
# https://stackoverflow.com/questions/54017285/difference-between-sort-rank-and-order
[1] 3 5 7 6 4 9 2 1 8
Question

Use getwd() to report the path to R’s current working directory

Since your computer is unique, you shouldn’t see exactly the same output as this, but it should be similar

[1] "/home/will/Documents/Classes/SDS 100"
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 Enter. R should read what you wrote in that text file!

readLines("/home/will/Documents/Classes/SDS 100/paths.txt")
[1] "R is helpful, but needs very specific directions."
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 Enter.

readLines("paths.txt")
[1] "R is helpful, but needs very specific directions."