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.

You should see the following output in your console:

[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, locate the code chunk that looks like this

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.

[1] 37
Question

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.

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

[1] 7
Question

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!

Once you create the vector daily_temps and sort it in descending order, you should see the following output in your console:

[1] 80 79 78 75 70 69 68 66 66
Question

Use this cheatsheet 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

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

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

readLines("/home/will/Documents/Classes/SDS 100/paths.txt")
[1] "R is helpful, but needs very specific directions."
Question

Use the readLines() function to print out what you stored in the paths.txt 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")

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