produce examples (can use built in data-sets like mtcars, diamonds, penguins)
#Example of "date-time" using "air quality" data-set
#Installing "air quality" data-set
data(airquality)
#Making a new column for "Year" to be set as 1973
airquality$Year <- 1973
#Combining "Month", "Date", and "Year" to be in new "Date" column
airquality$Date <- as.Date(paste(airquality$Year, airquality$Month, airquality$Day, sep = "-"))
#Making a graph based on Temperature changing over Time
# Installing ggplot
library(ggplot2)
#Plotting the graph of Temperature over Time
ggplot(airquality, aes(x = Date, y = Temp)) +
geom_line(color = "purple") +
geom_point(color = "darkgreen", size = 2) +
labs(title = "Temperature Changing Over Time",
x = "Date",
y = "Temperature (C)") +
theme_minimal()
#Example of "character" using "mtcars" data-set
#Installing "mtcars" data-set
data("mtcars")
#Transforming numeric column of vs engine into factor for character
mtcars$vs <- factor(mtcars$vs, labels = c("V Shaped Engine", "Straight Shaped Engine"))
#Changing the "vs" factor into a character
mtcars$vs <- as.character(mtcars$vs)
#Example of "numeric" using "diamond" data-set
#Loading "diamond" data-set
data(diamonds)
#Make a new column for the converted data
diamond_cut <- diamonds
#Changing the "cut" column into a numerical form (1-5, with 5 as best and 1 as worst)
diamond_cut <- diamond_cut
#mutate (
#cut = as.numeric(cut)
#)
#Example of "boolean" using "mtcars" data-set
#Loading "mtcars" data-set
data(mtcars)
#Making a new column for tested data
mtcars_data <- mtcars
#Creating a boolean test on "qsec" column if car is faster than 17.5 seconds to finish 1/4 mile
mtcars_qsec <- mtcars$qsec > 17.5
#Example of "array" using the categories "cut" and "clarity" from "mtcars" data-set
#Installing "mtcars" data-set
data("mtcars")
#Choosing "mpg", "hp" and "qsec" as the vectors used in building array from data-set
mpg_data <- mtcars$mpg
hp_data <- mtcars$hp
qsec_data <- mtcars$qsec
#Combining all the data of the chosen columns into one data-set, and make an array
data_array <- c(mpg_data, hp_data, qsec_data)
mtcars_array <- array(data_array, dim = c(length(mpg_data), 3),
dimnames = list(rownames(mtcars), c("MPG", "HP", "QSec")))
#Example of "vector" using "iris" data-set
#Installing "iris" data-set
data(iris)
#Choosing the "Sepal Width" column to be used for the vector
sepal_width <- iris$Sepal.Width
#Determining the data type of vector was used & how long the vector is
typeof(sepal_width)
## [1] "double"
length(sepal_width)
## [1] 150
#Example of "data-frame" using "air quality" data-set
#Installing "air quality" data-set
data("airquality")
#Looking at the mean of Solar Radiation from data-set
mean(airquality$Solar.R, na.rm = T)
## [1] 185.9315
#There's other options to use within a data-frame, but that is just one option
#Example of "list" using "penguins" data-set
#Installing "penguins" data-set
data("penguins")
#Making a "list" separating each penguin by species to show number of variables per species
listing_penguins <- split(penguins, f = penguins$species)
#Example of "tibble" using "iris" data-set
#Installing "tidyverse" package
#install.packages("tidyverse")
#Installing "iris" data-set and "tibble"
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.4 ✔ tibble 3.2.1
## ✔ purrr 1.0.4 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
data(iris)
#Using "tibble" to find length of petals within each species of flower in "iris" data-set
petal_vs_species <- tibble(
Petal.Length = iris$Petal.Length,
Species = iris$Species)