5/22/23
Important!
Create a folder for this course on your computer
Give it a short and meaningful title (e.g., Scope and Methods)
Store your data, R files, and any other helpful info from the course in it
Know where it is located (i.e., know the path)
Example Paths to Folders:
Better but still can have issues with replication and lengthiness
Use the R command setwd() to specify the “working directory”
Best method
“File” > “New Project” > “Existing Directory” > “Browse” > “Create Project”
Only need to open project to access your files
Top Left: Usually RScripts/RMarkdown files
Top Right: Environment where loaded objects will be shown
Bottom Left: Console (can run code–but this will not necessarily be as reproducible)
Bottom Right: Plots, Help, Files in Working Directory
As (social) scientists, we want our work to be reproducible
Important to save work such that another person could follow your exact steps and would get the same results
Use RScript files to write and save code
Similar to RScript files
But also allows you to include text
Produces good output for Problem Sets
Knit your RMarkdown frequently (i.e., at each step of creation)
Often small errors can cause issues
Open RStudio
Try to open a new RMarkdown document
Which parts of the document seem to include R code?
See if you can figure out how to insert a new code chunk (a section that R reads as code)
Arithmetic:
Addition: +
Subtraction: -
Multiplication: *
Division: /
Exponents: x^y (x to the power of y)
Go to R and open an RScript file
Set your working directory to your course folder by writing out code in the RScript
What is the output of the following lines of code?
Standard format:
Function help:
Type “?functionname” and run that line of code
. . .
Addition: sum()
sum(1, 2)
Average: mean()
Square Root: sqrt()
Minimum: min()
; Maximum: max()
Try running the following code:
How would you calculate this value using an R function?
What is the average of these three numbers?
c()
(turns a comma separated list of numbers into a vector)
Determine length of vector: length()
Return unique items in vector: unique()
begin:end
: creates a vector containing the sequence of integers from begin to end
seq(a, b, by = c)
: creates a vector containing the sequence of integers from a to b in steps of size c
x <- y
: assign y to object xMostly work with csv files (similar to Excel spreadsheets)
Load with some variant of read.csv()
function
This is where setting your working directory becomes important
setwd(path/to/course folder)
Section
Other students
?
operator in RStudio (run ?functionname
)
stackoverflow.com
I’m also happy to meet or to answer questions over email
Types of objects in R
If statements
Summarizing data
Missing data
For loops
Introduction to R