summary(cars) speed dist
Min. : 4.0 Min. : 2.00
1st Qu.:12.0 1st Qu.: 26.00
Median :15.0 Median : 36.00
Mean :15.4 Mean : 42.98
3rd Qu.:19.0 3rd Qu.: 56.00
Max. :25.0 Max. :120.00
Math 361
This is a Quarto document. Quarto is a multi-language open-sourced scientific and technical publishing system that lets you integrate narrative text, code, mathematical equations, figures, tables, citations, cross-references, etc. to create high-quality articles, reports, websites, blogs, books, presentations, and more.
In this course, we will be using Quarto to execute R code, but Quarto is compatible with multiple languages including Python and Julia.
Before we get started, let’s change two Settings:
Find the gear/sprocket icon next to the Render button (on the same line as the save, spellcheck, and search icons)
Click the gear/sprocket icon and then select Preview in Viewer Pane.
Click the gear/sprocket icon again and select Chunk Output in Console
Organizing and communicating your work is an extremely important aspect of any science. In most cases that is the main objective of statistical work. We want to use mathematical and statistical theory as well as data to build understanding and insight into the world around us. RStudio makes the process of organizing and communicating work with math, code, and data much easier by using Quarto.
How does Quarto make it easier?
Quarto allows us to produce documents that “Render” together content and output from embedded R code. That is, we can do both computational work and writing about results in one document — this saves us from a lot of tedious work and errors!
To help us compare how the code in the Quarto document produces what appears in the html document, click the Render button (blue arrow between search and settings icons above). Once the .html renders, you should be able to view the .qmd document on the left and the html version that is now previewed in the viewer pane on the right.
Looking in the Quarto document (not the rendered html), we can see that an R code chunk has been created below; an R code chunk will always start and end with three back ticks, and the code you want to run is included in the lines in between the back ticks. The first line of the code chunk will also always include r inside a set of brackets to tell Quarto that the code you’ve written is in the R programming language. Quarto also allows you to run code that’s written in other languages such as Python, SQL, etc., but we won’t be doing that in this course!
The embedded R code we’ve included here summary(cars) returns a quick summary on the 2 variables contained in a data set named cars.
summary(cars) speed dist
Min. : 4.0 Min. : 2.00
1st Qu.:12.0 1st Qu.: 26.00
Median :15.0 Median : 36.00
Mean :15.4 Mean : 42.98
3rd Qu.:19.0 3rd Qu.: 56.00
Max. :25.0 Max. :120.00
When looking at the html document, we can see that only the code itself summary(cars) is visible, but not the syntax (i.e. the back ticks, brackets, etc) that created the code chunk. The html also includes the output from the code, immediately after the code itself.
You can also embed plots, for example:
Note in the .qmd that the echo: false option was added to the code chunk to prevent printing of the R code that generated the plot. We also included a label for this code chunk pressure-plot. Labeling code chunks is not necessary, but it is often considered good workflow practice.
R can be used as a calculator. Basic operations include addition/subtraction 3 + 2, multiplication/division 3*2, and exponentiation 3^2. Try typing each of these commands in the console and verify that you get the output you expect. In R, parentheses are not automatically read as multiplication, so 3(2 + 1) will produce an error; you instead must type 3*(2 + 1).
“Objects” are the term for where values are saved in R. We assign values to objects using the “assignment operator” <-. For example, we can assign the value 9 to the object x with the code x <- 9. You can think of the assignment operator as an arrow pointing left; it takes the value on the right, and saves it into the object on the left. Run the following code chunk by clicking the green arrow on the right hand side of the code chunk.
x <- 9Check the “Environment” pane (the top-right pane in your RStudio window), and you should see that it now includes the object x. This means that R now knows what x is, and you can use x to perform other operations. Run the code chunk below and examine the output. Play around with other operations you can perform with x (division, exponentiation, etc.)
x*2[1] 18
The syntax c() (which stands for ‘concatenate’) allows us to create a list of values. The following code creates a list called L1 that contains the numbers 1 through 5. Notice that just running the first line of code only creates the object but does not produce any output. Once an object is created in R, we can “print” out its contents by running a line of code with the name of the object.
L1 <- c(1, 2, 3, 4, 5)
L1[1] 1 2 3 4 5
Mathematical operations are applied to each element of a list. For example, L1 - 2 subtracts 2 from every element in L1.
L1 - 2[1] -1 0 1 2 3
R syntax abides by the usual order of operations: 3 + 2*L1 first multiplies every element in L1 by 2, and then adds 3.
3 + 2*L1[1] 5 7 9 11 13
There are several ways to quickly create sequences. Instead of typing out each number 1, 2, 3, 4, 5 above, we could use the code c(1:5). The code c(21:26) would create a list with the numbers 21, 22, 23, 24, 25, 26. Explore what the code seq(from = 1, to = 10, by = 2) produces.
c(1:5)[1] 1 2 3 4 5
c(21:26)[1] 21 22 23 24 25 26
seq(from = 1, to = 10, by = 2)[1] 1 3 5 7 9
seq() is what we call a function. Functions perform tasks in R. Just like in math, they take inputs (which in R we call arguments) and return an output. Above, the code seq(from = 1, to = 10, by = 2) had three arguments (from, to and by), and its output was a vector of numbers. In more familiar mathematical language, you can think of seq() as being a function \(f(x, y, z)\), and we are asking R to evaluate this function \(f\) for \(x = 1\), \(y = 10\) and \(z = 2\). That is, R gives the result of \(f(1, 10, 2)\).
Once you are more familiar with the built-in functions in R and what arguments they require, you can often leave off the names of the arguments. That is, seq(1, 10, 2) would produce the same result as the code we ran above. Using the help function in R can help remind you what the required arguments are for a specific function, what order they should be in, and what the default values are. Typing ?seq in the console will make the help page for the function seq() pop up in the lower-right pane in R Studio. However, Google is often a more helpful resource than the help window!
R has built in functions that operate on lists. For example, sum(L1) will sum up all the elements in L1, max(L1) will return the largest value in the list, etc. Recall that we defined L1 to be a vector with elements 1, 2, 3, 4, 5. Therefore, we expect sum(L1) to return the value 15, since 1 + 2 + 3 + 4 + 5 = 15.
sum(L1)[1] 15
max(L1)[1] 5
Complete the following series of tasks. Remember to render early and render often.
Let’s make some adjustments to the YAML at the top of this document:
subtitle: goes right below title:.Insert an R code chuck below, and write a line of code that creates an object y and assigns it the value 45. Note, you can create a code chunk by clicking the green “+C” icon in the toolbar above. Hint: see the code chunk create_x above.
y <- 45Change the R code chunk options below to eval: true or alternatively you can simply remove , eval: false from the code chunk options. Either will have the same effect. render the document and observe the output of x*y. Is it what you expected? Note: This depends on completing Task 2 correctly.
x * y[1] 405
405[1] 405
Create a list L1 that contains all the even integers from -10 to 10. Use the seq() function instead of typing the numbers manually. Verify that L1 contains 11 elements by using the function length() to count the number of elements in L1.
L1 <- seq(from = -10, to = 10, by = 2)
length(L1)[1] 11
print(L1) [1] -10 -8 -6 -4 -2 0 2 4 6 8 10
Create a second list called L2 that computes the absolute value of each of the values in L1, and print out L2 to verify that it performed the operation you expected.
Hint: the R function for computing the absolute value is
abs().
Note: we use the language “print ___” to mean “tell R to show the values stored in the object named ____.” You don’t need to physically print the results :)
L2 <- (abs(L1))
print(L2) [1] 10 8 6 4 2 0 2 4 6 8 10
Use the function min() to compute the minimum value in L1. Compare this to the minimum value in L2.
BONUS: store these two values in a new list called
minsusing thec()function, and print the results of the new objectmins
mins <- c(min(L1),min(L2))
print(mins)[1] -10 0
Once you have completed Tasks 1 - 6, render the document one last time. Look over the .html document to make sure that everything is visible and formatted as you intended. To submit this lab assignment, you will upload your Lab_01.html file to Canvas under the Lab 01 assignment. This file exists in the MATH_361 folder you created on your computer.
To submit on Canvas, follow these steps:
In the MATH 361 Canvas site, navigate to Lab 01 under Assignments
Click “Start Assignment”
Click “Upload File”
Click “Choose File”
In the pop-up window, navigate to your MATH_361 folder and select your html file
Click “Submit Assignment”
RMarkdown also makes it easy to seamlessly type mathematical notation and formulas, using what’s known as LaTeX code. LaTeX (pronounced LAY-tekh, or LAH-tekh) is a tool for typesetting professional scientific documents. It has a broad range of capabilities and is a software system in and of itself, but its basic functioning for typing mathematical notation is pretty easy to pick up. You won’t be expected to type your own formulas in this course, but you may nonetheless find it interesting or useful to know the basics.
To type in mathematical notation in an RMarkdown document, use single dollar signs: \(x^2 + y^2 = 9\). To center an equation, use double dollar signs: \[x^2 + y^2 = 9\].
Here is some common mathematical notation that will appear in this class (see .qmd for how to typeset these symbols, see rendered .html for how they appear when formatted):
See cheatsheet on Canvas for more extensive list. And remember: Google is your friend!