Week 1
Data Wrangling and Visualization
R Workshop
NotePreview: dwv_w1_workshop.R
dwv_w1_workshop.R
#Title: DWV Workshop 1.2.R
#Author: Jack Reilly
#Date: 8.28.25
#Purpose: A Very Basic Introduction to R + Constructing Data Frames
#Requires: Nothing
#Output: Nothing
#R is an object oriented statistical programming language.
#What does this mean? You can store whatever* you want in an object, like a number:
myfirstobject<-2
myfirstobject
#Things in quotes indicate "strings"
mysecondobject<-"Hello"
mysecondobject
#You can also break lines with a semi-colon
mythirdobject<-"Bazinga"; mythirdobject
#You can also store vectors of numbers using the "combine" operator
manynumbers<-c(1,2,3); manynumbers
#Once you have many objects, you can do things with them together!
#R is one of the more complex basic calculators you'll ever see:
a<-1
b<-2
a+b
#Note that, if you want to save the results of any calculation, just point it to an object:
c<-a+b
#You can also perform pre-defined functions on objects. For instance:
mean(manynumbers)
mean(4, 5, 6, 10) #oops! why?
#If you want to know what a function does, you can ask R!
#Let's read a help file
?mean
#Look at the help file - only the *first* argument is the object to take a mean from.
#Other argumetns are *other* things
mean(c(4,5,6,10))
#So, last time we talked about R and objects in R.
#Let's expand a little bit. You can do other math - and math-like - things
2^2
sqrt(9)
#If you are unsure about the order of operations, include parentheses!
4^3
64^(1/3)
#Sequences
3:9
seq<-3:9
#Repeats
rep(2, 6)
#Functions with multiple arguments
#Sampling
?sample
#Let's roll a die 10 times.
sample(1:6, size = 10, replace = T)
#How about a coin 100 times?
sample(1:2, size = 100, replace = T)
#If you are unsure of the order of operations, it's always a good idea to use parentheses!
#We are PA, SS, IR - we care about code being predictable and clear. There aren't extra points for using fewer characters or being too fancy
5:2*3+10
#In the above, the *sequence* is calculated first, generating four numbers, each of which is then multiplied by 3 and then added to 10
#This does the same thing, but probably looks more intuitive
(5:2)*3+10
#Now, we're set with the basic math.
#Let's talk about data frames.
#First variable: age. Collect the ages of 10 people and concatenate them (using the c() command) into an object called "age"
age <- c(21,20,20,19,21,19,19,19,20,23)
#Have R show you what's in the "age" object
age
#Second variable: fruit. Collect the favorite fruits of 10 people and concatenate them (using the c() command) into an object called "fruit". Note that string variables (variables that are not numbers or true/false statements) should be recorded inside quotation marks
fruit <- c("Apple", "Grapefruit", "Strawberry", "Grape", "Grape", "Lemon", "Lime", "Tomato", "Watermelon", "Peach")
fruit
#Third variable: a true or false statement about whether or not someone is a New Yorker.
york <- c(TRUE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE)
york
#Put all three variables together in a single data frame
my.data <- data.frame(age, fruit, york)
#Have R show us the data frame
my.data
#Have R just show us the fruit variable
my.data$fruit
#Have R just show us the second column (the fruit variable)
my.data[,2]
#Have R just show us the second row (the second respondent)
my.data[2,]
#Have R just show us the cell in the eighth row, second column (the fruit variable)
my.data[8,2]
#Replace the item in the eighth row, second column with "Grape"
my.data[8,2]<-"Grape"
#Show the data again
my.data
#show 2 through 4th respondents, 2 through 3 variables
my.data[2:4,2:3]
#Find the average age of individuals in the dataset
mean(my.data$age)
#Find the standard deviation of individuals' age in the dataset
sd(my.data$age)
#Show a histogram of age
hist(my.data$age)
#you can also load external datasets!
nps<-read.csv("~/desktop/working/dwv/npsvisitation0910.csv")
nps<-read.csv(file.choose())
#View the data (in RStudio)
View(nps)Assignment 1 Solutions
Download answers to Assignment 1 Here
NotePreview: dwv_ps1_solutions.R
dwv_ps1_solutions.R
#Title: Assignment 1.R
#Author: Jack Reilly
#Purpose: Answer Assignment 1 Questions
#Requires: Nothing
#Output: Nothing
# Question 1
aardvark <- 3
# Question 2
boomba <- 6
# Question 3
centauri <- aardvark + boomba
# Question 4
diabolical <- aardvark * boomba
# Question 5
ebullient <- c(4,5,6)
# Question 6
fastidious <- c(8,9,11)
# Question 7
george <- ebullient + fastidious
# Question 8
zoinks <- mean(fastidious)