5 + 3[1] 85.3 * 23.4[1] 124.02sqrt(16) # this is a comment.  R will not 'see' anything past a #, but you can use it to explain your code[1] 4For this exercise, you will work in groups, but everyone will work through the document and submit their solution.
Open RStudio and create a subfolder in your AE folder called “AE-01”
Go to the Canvas and locate your AE 01 assignment to get started.
Upload the ae-01.qmd files into the folder you just created. To submit this Activity, upload the .qmd and .html responses in the corresponding Canvas assignment.
The thing you are reading right now is a Quarto document (very similar to R Markdown if you’ve ever used that). Quarto runs inside RStudio. Quarto is a simple formatting syntax for authoring web pages, Word documents, PDFs, and many more file types. You can find the link to a useful Quarto guide here.
When you click the Render button at the top of this window an html file will be generated that includes both content as well as the output of any embedded R code chunks within the document. Not only can you embed the R code, you can embed the output produced by the R code. In this way, your analysis is fully reproducible and updatable. All of your homework assignments must be prepared using Quarto.
The nice thing about Quarto is that you can write prose (as I am doing now), mathematical equations using LaTeX syntax (like \(y_i = a + b x_i\)), and R code/output/plots all in one synthesized document. This makes it approximately 10,000 times easier to use than doing the same thing in Word or LaTeX. As you go through this introduction, I recommend that you look also at the .qmd file and your output file side-by-side to get an idea of how Quarto works. (Since you’re going to have to use Quarto this semester, you might as well start learning it now!)
In the future, I will typically give you a Quarto template to fill out. However, occasionally you will need to create your own Quarto document. When you want to start a new Quarto document, click “File > New File > Quarto Document…” Put yourself as author, and make sure to give it a descriptive title!
Click Render at the top of this document. Show Dr. Friedlander before moving on.
With R by your side, you will never need your TI-84 again. Consider the simple arithmetic in the chunk below. You can run this code chunk all at once by clicking the Run button, the sideways green arrow in the top right of the chunk. Notice the interactive nature of the output. You can click through to see the different pieces of output.
As you work through this document, you should Run each chunk as you come to it.
Run the code chunk below.
5 + 3[1] 85.3 * 23.4[1] 124.02sqrt(16) # this is a comment.  R will not 'see' anything past a #, but you can use it to explain your code[1] 4Look closely at how Quarto denotes the R code and the output. Also note in the .qmd file how I include R code as separate from prose. These are called “chunks”. The easiest way to add a new chunk is to click on the green “c” icon with a plus in the corner above, then choose “R”. R code that is not inside of a chunk will not be run by Quarto!
Create an R code chunk and compute the log of 10 using the function log. Why do you think the answer isn’t 1?
You can also save values to named variables, to be used later:
product <- 15.3 * 23.4 #save resultIf you save something like this, R will not show the output unless you expressly ask for it:
product #show result[1] 358.02The symbol “<-” is the assignment operator. Imagine that R maintains a warehouse filled with boxes containing all kinds of stuff. When you use “<-”, R will create a new box, write whatever name you put to the left of “<-”, and put whatever is to the right of “<-” in the box. Then whenever you want to go get the box, you can just use whatever name you gave it. In the example above, the box is called “product” and it contains the number 358.02 (the result of 15.3 times 23.4). When you just write “product”, R will go grab the box called “product” and dump it onto the screen. In the future, we will be giving names to entire datasets and using “functions” in R to analyze them.
If you’ve ever programmed before, “=” is the most common assignment operator. In R, you can use “=” as the assignment operator but there are cases where “<-” works and “=” doesn’t, so it’s good to get into the habit of using “<-” now.
Once variables are defined, they can be referenced with other operators and functions. Try executing each line of code individually by placing your cursor on the first line of the chunk below and pressing Ctrl+Enter (Cmd +Enter for Mac users); then do the same for the second line. (This is how you can run a single line within a larger chunk.)
.5 * product # half of the product[1] 179.01product^(1/4) # fourth root of the product[1] 4.349875You can also use inline R code in Quarto, which can be useful when calling defined variables. Did you know that the natural log of 358.02 is 5.8805889?
Only R code (and comments) should be inside chunks. Prose (interpretations/explanations/descriptions) should never be put inside a chunk; prose should be below or above the chunk, as I have done above (and continue to do throughout this document). You should also never cut-and-paste output or graphs into the chunks. The whole point of code chunks is that they contain the code and they’ll run the code (resulting in the output and/or graphs).
If you want to run something in R but don’t want it to appear in the Quarto document, simply run it in the Console in the lower-left quadrant of RStudio. Type the last line of R code above into the Console and see what happens.
Run the cell below three times and note what the output is. Then, Render the document and note what the output is. Are they the same?
product <- product *2
product[1] 716.04When you click the run button for a chunk, it simply runs the code in the console below. When you Render your Quarto document, it wipes your environment and re-runs the entire document from the beginning. For this reason, if you run your cells multiple times or out of order, you may get unexpected results when you Render your document. Before you submit your documents, always Render and then double check your answers to make sure that your answers match your explanations.