Skip to content
Share
Explore

Install R on MacOS

Operators are used to perform operations on variables and values.

R code (live class)

Five core principles

variable
data types
data structures (R)
function
control flow
## Ctrl+Enter = run code (cmd + enter on Mac)
## Ctrl+L = clear console

Helper

help(keyword)
?keyword

1. Variables

## ===== Concept 01 - Variables =====

name <- "Tony"
age <- 31
uni <- "Torrens"

rm(uni)

2. Data types

Index in R start at 1
## ===== Concept 02 - Data types =====
## number, character, date, factor(categorical), logical(true, false)

name <- "Tony"
age <- 31
degree <- "Engineer"
quote <- "I'm loving it"
tiktok <- FALSE

## ! negate
!tiktok

## How to create date 101
today <- "2025-10-04"
today <- as.Date(today)

## How to create factor 101
gender <- c("male", "female", "female")
gender <- as.factor(gender)

fruit <- c("orange", "apple", "apple", "lemon")
fruit <- as.factor(fruit)

table(fruit)

## Recap: class(), as.type(), is.type()
is.numeric(1)
is.null("")

3. Data structures

## ===== Concept 03 - Data structures =====
## 1. vector => can use for only 1 datatype (single datatype)
## 2. matrix
## 3. list
## 4. dataframe

## ----- vector example -----
friends <- c("toy", "jenny", "lisa", "jisoo", "rose")

## subset the vector
## [1] position
## [2] condition
## [3] name

ages <- c(37, 28, 25, 30, 31)

ages <- c(toy=37, jenny=28, lisa=25)

## ----- matrix example -----
m1 <- matrix(1:10, ncol = 5, byrow = TRUE)

m2 <- matrix(
c(3,4,5,8,10,12),
ncol = 3,
byrow = TRUE
)

## wrap variable in () to show its value
(m22 <- matrix(
c(13,4,5,8,10,12),
ncol = 3,
byrow = TRUE
))

## Matrix multiplication
m1 <- matrix(1:6, ncol = 3)
m2 <- matrix(c(5,5,6,6,9,10), ncol = 2)

m1 %*% m2

## ----- list example ----- key - value pair
tony <- list(
fname = "Tanapoom",
age = 31,
dob = "06-Aug"
)

tony$fname

## ----- dataframe example -----

id <- 1:5
friends <- c("toy", "lisa", "jisoo", "jenny", "david")
age <- c(37, 25, 30, 22, 28)
city <- c("bangkok", rep("london", 3), "tokyo")

## dataframe
df <- data.frame(id,
friends,
age,
city)

View(df)

## subset by position
df
df[3, ]
df[3, c(2,4)]

df[ , 1:2]
df[4:5, 4]
df[1:3, 2]

df$city
df[df$age < 30, ]
df[df$age < 30, "friends"]

## Use SQL syntax in R
sqldf("select avg(age) from df")

str(df)
summary(df)
ncol(df)

df$city <- as.factor(df$city)
str(df)
summary(df)

## create column
df$reading <- c(T,T,T,F,F)
df
df[df$reading, ]
df[df$reading == F, ]
df[!df$reading, ]

## remove column
df$reading <- NULL
df

## export column
write.csv(df, "friends.csv",
row.names = FALSE)


df <- read.csv("friends.csv")
df

Vectors

A list of the same type of items. To combine the list of items to a vector, use the c() function and separate the items by a comma.
## Vector of characters/strings
fruits <- c("banana", "apple", "orange")

## Print fruits
fruits

[1] "banana" "apple" "orange"

## Sequence generation
seq(from = , to = 100, by = 2)

Factors

Factors are used to categorise data. Examples of factors are:
Demography: Male/Female
Music: Rock, Pop, Classical, Jazz
## Create a factor
music_genre <- factor(c("Jazz", "Rock", "Classic", "Classic", "Pop", "Jazz", "Rock", "Jazz"))

## Print the factor
music_genre

[1] Jazz Rock Classic Classic Pop Jazz Rock Jazz
Levels: Classic Jazz Pop Rock

## ---------
## To only print the levels, use the levels() function:
levels(music_genre)

[1] "Classic" "Jazz" "Pop" "Rock"

Matrices

A matrix is a two-dimensional data set with columns and rows.
A matrix can be created with the matrix() function. Specify the nrow and ncol parameters to get the number of rows and columns.
thismatrix <- matrix(c("apple", "banana", "cherry", "orange"), nrow = 2, ncol = 2)

thismatrix

## Output
[,1] [,2]
[1,] "apple" "cherry"
[2,] "banana" "orange"
c() function is used to concatenate items together.

4. Function


5. Control flow



Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.