R
April 6, 2023
Technical Team
Yan Luo, Arthur Ang
Exercise
Lesson
Assignment
Agenda
Download R
Data Types
ggplot2
ANOVA
Lesson
01
Download R
Install R: https://cran.r-project.org/
Install RStudio: https://posit.co/download/rstudio-desktop/
Apps@Stern: https://apps.stern.nyu.edu/
TutorialsPoint: https://www.tutorialspoint.com/execute_r_online.php
Hello World
Terminal: Rscript fileName.R
Data Types
02
Data Types - Vector Objects
Vectors
Lists
Matrices
Arrays
Factors
Data Frames
Data Type | Example | Syntax |
Logical | TRUE, FLASE | v <- TRUE |
Numeric | 1, 3.14, -3/4 | v <- 1 |
Integer | -2L, 0L, 2L | v <- -2L |
Complex | 3+2i | v <- 3.2i |
Character | “ActSoc”, ‘b’, ‘3.14’ | v <- “ActSoc” |
Raw | “ActSoc” 41 63 74 53 6f 63 | v <- charToRaw(‘ActSoc”) |
class(v) to check data type, typeof(v) gives "logical", "integer", "double", "complex", "character", "raw" and "list", "NULL", "closure" (function), "special" and "builtin" (basic functions and operators)
Data Types - Vectors
Vectors
Lists
Matrices
Arrays
Factors
Data Frames
# Create a vector.
apple <- c('red','green',"yellow")
print(apple)
# Get the class of the vector.
print(class(apple))
Data Types - Lists
Vectors
Lists
Matrices
Arrays
Factors
Data Frames
# Create a list.
list1 <- list(c(2,5,3),21.3,sin)
# Print the list.
print(list1)
Data Types - Matrices
Vectors
Lists
Matrices
Arrays
Factors
Data Frames
# Create a matrix.
M = matrix( c('a','a','b','c','b','a'), nrow = 2, ncol = 3, byrow = TRUE)
print(M)
Data Types - Arrays
Vectors
Lists
Matrices
Arrays
Factors
Data Frames
# Create an array with one dimension with values ranging from 1 to 24.
thisarray <- c(1:24)
thisarray
# Create an array with more than one dimension.
multiarray <- array(thisarray, dim = c(4, 3, 2))
multiarray
Data Types - Factors
Vectors
Lists
Matrices
Arrays
Factors
Data Frames
# Create a vector.
apple_colors <- c('green','green','yellow','red','red','red','green')
# Create a factor object.
factor_apple <- factor(apple_colors)
# Print the factor.
print(factor_apple)
print(nlevels(factor_apple))
Data Types - Data Frames
Vectors
Lists
Matrices
Arrays
Factors
Data Frames
# Create the data frame.
BMI <- data.frame(
gender = c("Male", "Male","Female"),
height = c(152, 171.5, 165),
weight = c(81,93, 78),
Age = c(42,38,26)
)
print(BMI)
ggplot2
03
ggplot2
ANOVA
04
ANOVA
One-Way Anova Results
Post-Hoc Tests
Homework
05
Homework
Resources
Code Used in Workshop
https://drive.google.com/drive/folders/1QRJQAKF49FMRen5o2sfY9R5nIYsaywV1?usp=sharing
One-way ANOVA:
https://statsandr.com/blog/anova-in-r/
ggplot2 Visualization:
http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html
R Tutorial:
Email actrlsoc@gmail.com
Website nyuactsoc.com
Discord discord.gg/Nf8TqAVBfa
Instagram @nyuactsoc
Facebook @actrlsoc