Lab 1: R, Rstudio and R Markdown SOC 225: Data & Society [Griffin Mcgrath]
2023-06-29
Agenda 1: Getting started: Editing and running code 2: Functions and help 3: Objects 4: Basics about R Markdown References Agenda Getting started: Editing and running code Functions and help Objects Basics about R Markdown 1: Getting started: Editing and running code There are several ways we can run code:
Highlight lines in the editor window and click Run at the top or hit Ctrl + Enter or CMD + Enter (Mac) to run them all. With your caret on a line you want to run, hit Ctrl + Enter or CMD + Enter (Mac). Note your caret moves to the next line, so you can run code sequentially with repeated presses. Type individual lines in the console and press enter. In R Markdown documents, click within a code chunk and click the green arrow to run the chunk. The button beside that runs all prior chunks. Question 1.1: run the following lines of code.
123 + 456 + 789 ## [1] 1368 sqrt(400) ## [1] 20 Now run the following code in console: (11-2. This code obviously is incomplete. What do you see in your console? + You may notice that here, R is used as a calculator. Now, follow the instructions on this website R as a calculator and play out with calculation in the console.
2: Functions and help sqrt() is an example of a function in R.
If we didn’t have a good guess as to what sqrt() will do, we can type ?sqrt in the console and look at the Help panel on the right.
Question 2.1: looking at the Help file of function sqrt(). ?sqrt Argument are inputs to a function. In this case, the only argument to sqrt() is x which can be a number or a vector of numbers.
Help files provide documentation on how to use functions and what functions produce.
3: Objects R stores everything as an object, including data, functions, models, and output.
Creating an object can be done using the assignment operator: <- When you’re typing inside of a code chunk (that’s where the code goes), you can use alt plus the minus key to quickly type the arrow.
Question 3.1: Create an new object called “new_object”. Assign value 144 to it. “new_object” <-144 new_object <- 144 Operators like <- are functions that look like symbols but typically sit between their arguments (e.g. numbers or objects) instead of having them inside () like in sqrt().
You can display or “call” an object simply by using its name:
new_object ## [1] 144 An object’s name represents the information stored in that object, so you can treat the object’s name as if it were the values stored inside.
new_object + 10 ## [1] 154 new_object + new_object ## [1] 288 Next, we are going to create a vector. A vector is a series of elements, such as numbers. You can create a vector and store it as an object in the same way. To do this, use the function c() which stands for “combine” or “concatenate”.
new_object <- c(4,9,16,25,36) new_object ## [1] 4 9 16 25 36 If you name an object the same name as an existing object, it will overwrite it.
Question 3.2: calculate the square root of vector new_object.
sqrt(new_object) ## [1] 2 3 4 5 6 The same principles can be used to create more complex objects like matrices, arrays, lists and dataframes (lists of equal-length vectors). Each element of the dataframe can be thought of as a column, and the length of each element is the number of rows.
Most data sets we work with will be read into R and stored as a dataframe, so the labs will mainly focus on working with these objects.
Let’s take a look at an example of data on covid-19. To do so, we will first have to install a package called “coronavirus”, developed by the Johns Hopkins University. Packages contain premade functions and/or data we can use. It is R’s strength that there are a wide variety of packages!
#setwd() #include=FALSE
You can make a new code chunk by using the insert menu above, typing the backtics by hand, or by pressing CMD+option+i on a Mac or CTRL + ALT + i on windows or linux.
Chunks have options that control what happens with their code, such as:
echo=FALSE: Keeps R code from being shown in the document. eval=FALSE: Shows R code in the document without running it. include=FALSE: Hides all output but still runs code. results=‘hide’: Hides R’s (non-plot) output from the document cache=TRUE: Saves results of running that chunk so if it takes a while, you won’t have to re-run it each time you re-knit the document. fig.height=5, fig.width=5: modify the dimensions of any plots that are generated in the chunk (units are in inches). Question 4.1: hide outputs in the chunk where we loaded package.
Sometimes we want to combine text with values. We do that using code in single backticks starting off with r.
Four score and seven years ago is the same as 87 years. Notice that R could also be used as a calculator.
We are able to reference an object we already saved in a chunk:
x <- sqrt(77) The value of x rounded to the nearest two decimals is 8.77.
Inserting in-line code with R helps you prevent silly mistakes and makes your coding much more efficient - you don’t have to manually update your hard-coded number if the calculation changes.
Question 4.2: mix in-line R calculations/references with text. You may not hard-code any numbers referenced.
Start your text here: x <- sqrt(10)
More about RMarkdown: R Markdown Cheat Sheet.
Question 4.3: Taking a look at the cheat sheet, and try to work on the following outputs.
Italicize this line
Make this line bold.
Strike through this line.
Create a block quote of the following poem: “No man is an island, Entire of itself, Every man is a piece of the continent, A part of the main.”
“No man is an island, Entire of itself, Every man is a piece of the continent, A part of the main.”
Finally, how do we take a look at the final output of our Markdown document?
Click on Knit, select Knit to HTML.
References Charles Lanfear, Introduction to R for Social Scientists
Managing Data Frames
Short Classic Poems