# ##########################################################
# R Foundations — full course R scripts
# https://rverseanalytics.com/courses/r-foundations
# ##########################################################



# ----------------------------------------------------------
# R Basics
# ----------------------------------------------------------

# R Basics - run real R, no install
# 1. R is a calculator
2 + 2
10 * 5
100 / 7
2 ^ 10
# 2. Store values in variables with  <-
price <- 20
quantity <- 3
price * quantity
# 3. Call a function: name(input)
sqrt(144)
round(3.14159, 2)
toupper("hello r")
# 4. Comments start with #  - R ignores them
radius <- 5
area <- pi * radius ^ 2
area
# 5. Your turn: Celsius to Fahrenheit
celsius <- 25
celsius * 9 / 5 + 32


# ----------------------------------------------------------
# Vectors in R
# ----------------------------------------------------------

# Vectors in R - the building block
# 1. Combine values with c()
ages <- c(23, 35, 41, 29, 52)
ages
names <- c("Ada", "Bo", "Cy")
names
# 2. Maths runs on every element at once
prices <- c(10, 20, 30)
prices * 1.2
prices - 5
# 3. Pick elements with [ ]   R counts from 1
ages[1]
ages[c(1, 3)]
ages[ages > 30]
# 4. Quick ways to build vectors
1:10
seq(0, 1, by = 0.25)
rep("x", 4)
# 5. Summarise a vector
scores <- c(88, 92, 79, 95, 84, 90)
length(scores)
mean(scores)
max(scores)


# ----------------------------------------------------------
# Data Frames in R
# ----------------------------------------------------------

# Data Frames - R's spreadsheet
people <- data.frame(name = c("Ada", "Bo", "Cy"), age = c(35, 41, 29))
people
# 2. Pull a column with $
people$age
mean(people$age)
# 3. A built-in dataset: mtcars (32 cars)
head(mtcars[, 1:4])
nrow(mtcars)
# 4. Filter rows:  [rows, columns]
mtcars[mtcars$mpg > 25, 1:4]
# 5. A grouped summary: mpg by gearbox
aggregate(mpg ~ am, data = mtcars, FUN = mean)


# ----------------------------------------------------------
# Your First Plot in R
# ----------------------------------------------------------

# Your First Plot in R - base graphics
# 1. A histogram: the shape of one variable
hist(mtcars$mpg, col = "#2f6fed", main = "Fuel efficiency", xlab = "Miles per gallon")
# 2. A scatter plot: two variables
plot(mtcars$wt, mtcars$mpg, pch = 19, col = "#17a2b8", xlab = "Weight (1000 lbs)", ylab = "Miles per gallon")
# 3. Add a regression trend line
plot(mtcars$wt, mtcars$mpg, pch = 19, col = "#17a2b8", xlab = "Weight", ylab = "MPG")
abline(lm(mpg ~ wt, data = mtcars), col = "#b02a37", lwd = 2)
# 4. A boxplot by group
boxplot(mpg ~ cyl, data = mtcars, col = "#2f6fed33", border = "#1b2a4a", xlab = "Cylinders", ylab = "MPG")


# ----------------------------------------------------------
# Basic Statistics in R
# ----------------------------------------------------------

# Basic Statistics in R
# 1. Summary statistics
scores <- c(88, 92, 79, 95, 84, 90, 76, 88)
mean(scores)
sd(scores)
summary(scores)
# 2. A frequency table
table(mtcars$cyl)
# 3. Correlation:  -1 to +1
cor(mtcars$wt, mtcars$mpg)
# 4. Your first t-test: does gearbox affect mpg?
t.test(mpg ~ am, data = mtcars)
# 5. A simple linear model
model <- lm(mpg ~ wt, data = mtcars)
summary(model)
