Function / Command What it does Example in lab
c() Combines values into a vector c(4, 5, 7, 9)
mean() Computes the mean of a variable/vector mean(my_numbers)
install.packages() Installs a package install.packages("lubridate")
library() Loads a package into the session library("lubridate")
Sys.Date() Returns today’s date today <- Sys.Date()
as.Date() Converts a string into a Date object as.Date("2024-06-28")
time_length() Computes length of a time difference time_length(birthday - today)
abs() Returns absolute value abs(time_difference)
read.table() Reads tabular data from a file or URL read.table(url, sep=",", header=FALSE, ...)
class() Tells the object type/class class(iris_data)
head() Shows the first rows of a dataset head(iris_data)
sd() Computes sample standard deviation sd(iris_data$Sepal.Length)
summary() Gives summary statistics summary(iris_data)
stem() Makes a stem-and-leaf plot stem(iris_data$Sepal.Length)
hist() Makes a histogram hist(iris_data$Sepal.Length)
boxplot() Makes a boxplot boxplot(iris_data$Sepal.Length)
plot() Makes a plot/scatterplot plot(iris_data$Sepal.Length, iris_data$Petal.Length)
pdf() Opens a PDF graphics device to save plots pdf("box.pdf")
dev.off() Closes the graphics device dev.off()
log() Applies log transformation log(maas$lead)
which() Returns row positions meeting a condition which(maas$lead > 450)
unique() Removes duplicate values unique(c(...))
map() Draws a map from the maps package map("county", "ca", add=TRUE)
points() Adds points to an existing plot points(b$x, b$y, cex=b$o3/mean(b$o3))
cut() Splits numeric values into intervals/groups cut(b$o3, c(...))
as.numeric() Converts an object to numeric form as.numeric(AQI_levels)
hist(mtcars$mpg)
boxplot(mtcars$mpg, horizontal = TRUE, add=T)

maas <- read.table("http://www.stat.ucla.edu/~nchristo/statistics12/soil.txt", header=TRUE)
head(maas)
##        x      y lead zinc
## 1 181072 333611  299 1022
## 2 181025 333558  277 1141
## 3 181165 333537  199  640
## 4 181298 333484  116  257
## 5 181307 333330  117  269
## 6 181390 333260  137  281
url <- "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"

iris_data <- read.table(
  url,
  sep = ",",
  header = FALSE,
  col.names = c(
    "Sepal.Length",
    "Sepal.Width",
    "Petal.Length",
    "Petal.Width",
    "Species"
  )
)

head(iris_data)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width     Species
## 1          5.1         3.5          1.4         0.2 Iris-setosa
## 2          4.9         3.0          1.4         0.2 Iris-setosa
## 3          4.7         3.2          1.3         0.2 Iris-setosa
## 4          4.6         3.1          1.5         0.2 Iris-setosa
## 5          5.0         3.6          1.4         0.2 Iris-setosa
## 6          5.4         3.9          1.7         0.4 Iris-setosa