Atomic vectors are simple one-dimensional collections of data.
# simple vector
die <- c(1, 2, 3, 4, 5, 6)
die
## [1] 1 2 3 4 5 6
# checking if something is a vector
is.vector(die)
## [1] TRUE
# vectors can have just one value
five <- 5
five
## [1] 5
is.vector(five)
## [1] TRUE
# checking length of vectors
length(five)
## [1] 1
length(die)
## [1] 6
# creating different types of vectors
int <- 1L
text <- "ace"
# vectors with multiple elements
int <- c(1L, 5L)
text <- c("ace", "hearts")
# math works on number vectors but not on text
sum(int)
## [1] 6
# sum(text) # this will output an error
Doubles store regular numbers (positive, negative and may contain decimals).
die <- c(1, 2, 3, 4, 5, 6)
die
## [1] 1 2 3 4 5 6
typeof(die)
## [1] "double"
Integers store whole numbers without decimal components. We can add L after the number to make it an integer.
int <- c(-1L, 2L, 4L)
int
## [1] -1 2 4
typeof(int)
## [1] "integer"
# floating-point errors example
# below example should ideally output 0 but instead gives 4.440892e-16
# which is very close to zero due to how R loses precision using double.
sqrt(2)^2 - 2
## [1] 4.440892e-16
Character vectors store text. Text should within quotes.
text <- c("Hello", "World")
text
## [1] "Hello" "World"
typeof(text)
## [1] "character"
typeof("Hello")
## [1] "character"
Logical vectors store TRUE and FALSE values (Boolean data). They can be used for comparisons.
3 > 4
## [1] FALSE
logic <- c(TRUE, FALSE, TRUE)
logic
## [1] TRUE FALSE TRUE
typeof(logic)
## [1] "logical"
typeof(F)
## [1] "logical"
Complex vectors store complex numbers. Raw vectors store raw bytes of data.
# Complex numbers have an imaginary component with i
comp <- c(1 + 1i, 1 + 2i, 1 + 3i)
comp
## [1] 1+1i 1+2i 1+3i
typeof(comp)
## [1] "complex"
# Raw vectors store raw bytes
raw(3)
## [1] 00 00 00
typeof(raw(3))
## [1] "raw"
Matrices are 2D arrays that store data in rows and columns.
# Create a matrix with 2 rows
m <- matrix(die, nrow = 2)
m
## [,1] [,2] [,3]
## [1,] 1 3 5
## [2,] 2 4 6
# Fill matrix row by row instead of column by column
m <- matrix(die, nrow = 2, byrow = TRUE)
m
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
# (Exercise 5.3) Creating a matrix for royal flush, which stores the name and suit of every card in a royal flush.
# Method - passing byrow parameter to matric function which is more intuitive
# Step 1: Create a character vector to store values
hand1 <- c("ace", "spades", "king", "spades", "queen", "spades", "jack",
"spades", "ten", "spades")
# Step 2: Ask R to fill the matrix row by row instead of column by column
matrix(hand1, nrow = 5, byrow = TRUE)
## [,1] [,2]
## [1,] "ace" "spades"
## [2,] "king" "spades"
## [3,] "queen" "spades"
## [4,] "jack" "spades"
## [5,] "ten" "spades"
matrix(hand1, ncol = 2, byrow = TRUE)
## [,1] [,2]
## [1,] "ace" "spades"
## [2,] "king" "spades"
## [3,] "queen" "spades"
## [4,] "jack" "spades"
## [5,] "ten" "spades"
Arrays are n-dimensional data structures (3D, 4D, etc) and
array
is an in-built function that creates an array.
# Create a 3D array with dimensions 2x2x3
ar <- array(c(11:14, 21:24, 31:34), dim = c(2, 2, 3))
ar
## , , 1
##
## [,1] [,2]
## [1,] 11 13
## [2,] 12 14
##
## , , 2
##
## [,1] [,2]
## [1,] 21 23
## [2,] 22 24
##
## , , 3
##
## [,1] [,2]
## [1,] 31 33
## [2,] 32 34
Lists are 1D structures that can store different types of R objects together.
# Creating a list with different types of elements
list1 <- list(100:130, "R", list(TRUE, FALSE))
list1
## [[1]]
## [1] 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
## [20] 119 120 121 122 123 124 125 126 127 128 129 130
##
## [[2]]
## [1] "R"
##
## [[3]]
## [[3]][[1]]
## [1] TRUE
##
## [[3]][[2]]
## [1] FALSE
# Lists can have complicated structure
# Double brackets [[]] show which element
# Single brackets [] show which subelement
# (Exercise 5.5) Use a List to Make a Card.
# Creating a playing card as a list.
# Store card face, suit, and value in a list
card <- list("ace", "hearts", 1)
card
## [[1]]
## [1] "ace"
##
## [[2]]
## [1] "hearts"
##
## [[3]]
## [1] 1
Data frames are 2D tables similar to Excel spreadsheets - columns can have different data types.
# Create a data frame with three cards
df <- data.frame(face = c("ace", "two", "six"),
suit = c("clubs", "clubs", "clubs"), value = c(1, 2, 3))
df
# Check the structure of a data frame
typeof(df)
## [1] "list"
class(df)
## [1] "data.frame"
str(df)
## 'data.frame': 3 obs. of 3 variables:
## $ face : chr "ace" "two" "six"
## $ suit : chr "clubs" "clubs" "clubs"
## $ value: num 1 2 3
# R converts strings to factors by default
# Prevent this with stringsAsFactors = FALSE
df <- data.frame(face = c("ace", "two", "six"),
suit = c("clubs", "clubs", "clubs"), value = c(1, 2, 3),
stringsAsFactors = FALSE)
# Note: Though its possible to create data frames by typing manually using data.frame its advisable to load large datasets from files instead and load them into IDE.