GET UP AND RUNNING
GET UP AND RUNNING TODAYS GOAL: GET ORIENTED TO RSTUDIO, MAKE A - - PowerPoint PPT Presentation
GET UP AND RUNNING TODAYS GOAL: GET ORIENTED TO RSTUDIO, MAKE A - - PowerPoint PPT Presentation
GET UP AND RUNNING TODAYS GOAL: GET ORIENTED TO RSTUDIO, MAKE A PROJECT FOLDER https://www.r-project.org https://rstudio.com RStudio will be the control center for your R Projects >_ Current Objects Document Files, Graphs,
TODAY’S GOAL:
GET ORIENTED TO RSTUDIO, MAKE A PROJECT FOLDER
https://www.r-project.org https://rstudio.com
RStudio will be the control center for your R Projects
Current Document
Console Objects
Files, Graphs, Help
Drives R Produces Plots Generates Documents RStudio
THE RIGHT FRAME OF MIND
TYPE OUT YOUR
CODE BY HAND
RSTUDIO AGAIN
Paper, Report, Analysis, Notes, etc, in RMarkdown
Console: Type or send code here, see results
Project files, Plots, Help
Inspect objects you create
TASK:
CREATE A PROJECT FOR YOUR NOTES
Use RMarkdown TO PRODUCE & REPRODUCE YOUR OWN WORK
This is what we want to end up with: nicely formatted text, plots, and tables in an HTML, PDF,
- r Word file
- 1. Lorem Ipsum
In a Literate Programming approach to documents, chunks of code are processed and replaced with their output
library(ggplot2) tea <- rnorm(100) biscuits <- tea + rnorm(100, 0, 1.3) data <- data.frame(tea, biscuits) p <- ggplot(data, aes(x = tea, y = biscuits)) + geom_point() + geom_smooth(method = "lm") + labs(x = "Tea", y = "Biscuits") + theme_bw() print(p) # Lorem Ipsum Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do *eiusmod tempor* incididunt ut labore et dolore magna- aliqua. Ut enimad minim veniam, quis nostrud exercitation
- ccaecat cupidatat non proident, sunt in culpa qui officia
This is what our file actually looks like: plain text, mixing words and chunks of code
Markdown is a way to put formatting instructions in plain-text documents
# Header Plain text *italics* **bold** `verbatim` Footnote.[^1] [^1]: The footnote.- 1. List
- 2. List
- Bullet 1
- Bullet 2
- 1. List
- 2. List
Once the chunks of R Code have all been run, a Markdown Processor turns the bits of marked-up plain text into properly-formatted
- utput in HTML, PDF, DOCX or
- ther file types.
What you type What you end up with
In a Literate Programming approach to documents, chunks of code are processed and replaced with their output
library(ggplot2) tea <- rnorm(100) biscuits <- tea + rnorm(100, 0, 1.3) data <- data.frame(tea, biscuits) p <- ggplot(data, aes(x = tea, y = biscuits)) + geom_point() + geom_smooth(method = "lm") + labs(x = "Tea", y = "Biscuits") + theme_bw() print(p) # Lorem Ipsum Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do *eiusmod tempor* incididunt ut labore et dolore magna- aliqua. Ut enimad minim veniam, quis nostrud exercitation
- ccaecat cupidatat non proident, sunt in culpa qui officia
- 1. Lorem Ipsum
In a Literate Programming approach to documents, chunks of code are processed and replaced with their output
An Rmd document lets you keep your code and notes together in plain text And produce good-looking
- utput in a range of formats
An Rmd document lets you keep your code and notes together in plain text And produce good-looking
- utput in a range of formats
An Rmd document lets you keep your code and notes together in plain text And produce good-looking
- utput in a range of formats
Header section provides metadata and sets options
Code chunk
Text with Markdown formatting In RStudio, code chunks can be "played" one at a time Chunks are replaced by their- utput when the
Code chunks can have their
- wn names and options
RStudio will do all the work for you when it comes to processing your document—i.e., getting it from plain-text Rmd to HTML, Word, or PDF.
- 1. Lorem Ipsum
In general, your code is what’s “real” in your project, not the objects you create.
Consider not showing your
- utput inline
GETTING ORIENTED
The Tidyverse
Draw graphs Nicer data tables Tidy your data Get data into R Cool functional programming stuff Action verbs for manipulating data library(socviz)Course-Specific Library
CODE YOU CAN TYPE AND RUN
## Inside chunks of code, lines beginning with ## the hash character are comments my_numbers <- c(1, 1, 4, 1, 1, 4, 1) my_numbers ## [1] 1 1 4 1 1 4 1OUTPUT
What R Looks Like
FOUR THINGS TO KNOW ABOUT R
1: Everything has a Name
FALSE TRUE Inf for if break functionSome names are forbidden
my_numbers data p- 2. Everything is an Object
You create objects by assigning a thing to a name
named thing "gets" this stuff
You create objects by assigning a thing to a name
The assignment operator performs the action of creating objects. Use a keyboard shortcut to type it:
- ption - Mac
alt - Windows
- 3. You do things with named objects
using functions and operators
my_numbers <- c(1, 2, 3, 1, 3, 5, 25)named thing "gets" this stuff c() is a function that takes comma-separated numbers or strings and joins them together into a vector
take arguments, perform actions, produce outputs
mean()Functions have parentheses at the end of their name. This is where the inputs,
- r arguments go.
Named argument. “Calculate the mean of what, please?” These names are internal to functions. “Take this object …”
Functions
If you just write the name of the input, R assigns it to the function’s arguments in order. Look at the function’s help page to see the order it expects its arguments.
take arguments, perform actions, produce outputs
Functions
You can assign a function’s
- utput to a named object
Objects you create exist until you overwrite or delete them
rm(my_numbers) my_numbers my_numbers <- c(1, 2, 3, 1, 3, 5, 25)Objects are of different classes
class(my_numbers) numeric character factorVectors
matrix data.frame tibbleArrays
lm glmModels
Things to try on Objects
class(my_numbers) table(my_numbers) x <- c(my_numbers, 5) mean(c(my_numbers, my_numbers))Notice that these are functions How do x and y differ?
y <- c(my_numbers, "hello")Functions can be nested, and will be evaluated from the inside out.
Some operators
+, -, *, /, ^
Arithmetic
<-
Assignment ("gets")
=
- r
&, &&, |, ||, !
Logical
%*%, %in%, %>% Special <, >, <=, >=, ==, !=
Relational
The pipe operator
mean(my_numbers) my_numbers %>% mean()This will be very convenient later on
round(mean(my_numbers)) my_numbers %>% mean() %>% round()"and then"
%>%
- 4. R will be Frustrating
We’re going to be joining a lot of objects and functions together
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point()"+"
goes here
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + geom_point()not here
- 4. R will be Frustrating
RStudio tries its best to help. Learn to attend to what it’s trying to tell you
LET’S GO
Named thing gets … … the output of this function … … using these arguments Objects created by ggplot() are unusual in that you can “add” things to them, and they will work as though you wrote all the code at once.
NEXT
WEEK
- 1. Readings will be provided
in the Slack
- 3. Finish setting up your
notes.Rmd file. Use it to take notes on the reading
- 4. Questions or glitches with
R or RStudio: Ask in the Slack
- 2. Slides will be on the
Website