5.1 Atomic Vectors
#Creates a vector named die that contains six numeric elements
die <- c(1, 2, 3, 4, 5, 6)
die
## [1] 1 2 3 4 5 6
#Tests whether the object is an atomic vector
is.vector(die)
## [1] TRUE
#Creates a vector with only 1 value
five <- 5
five
## [1] 5
#Tests whether the object is an atomic vector
is.vector(five)
## [1] TRUE
#Returns the length of the vector
length(five)
## [1] 1
#Returns the length of the vector
length(die)
## [1] 6
#Creates an integer vector
int <- 1L
#Creates a character vector
text <- "ace"
#Creates an integer vector
int <- c(1L, 5L)
#Creates a character vector
text <- c("ace", "hearts")
#Calculates the sum of vector "int"
sum(int)
## [1] 6
#Calculates the sum of vector "text" - will returns error
#sum(text)
5.1.1 Doubles
#Creates a vector named die that contains six numeric elements
die <- c(1, 2, 3, 4, 5, 6)
die
## [1] 1 2 3 4 5 6
#Identifies the type of an object
typeof(die)
## [1] "double"
5.1.2 Integers
#Creates an integer vector
int <- c(-1L, 2L, 4L)
int
## [1] -1 2 4
#Identifies the type of an object
typeof(int)
## [1] "integer"
#Demonstrates a situation when R does not return zero as we normally expected
sqrt(2)^2 - 2
## [1] 4.440892e-16
5.1.3 Characters
#Creates a character vector
text <- c("Hello", "World")
text
## [1] "Hello" "World"
#Returns type of an object
typeof(text)
## [1] "character"
#Returns type of an element in the "text" vector
typeof("Hello")
## [1] "character"
5.1.4 Logicals
#Logicals - returns TRUE or FALSE
3 > 4
## [1] FALSE
#Creates a logical vector
logic <- c(TRUE, FALSE, TRUE)
logic
## [1] TRUE FALSE TRUE
#Returns type of an object
typeof(logic)
## [1] "logical"
#Returns type of an element in the "logic" vector
typeof(F)
## [1] "logical"
5.1.5 Complex and Raw
#Creates a complex vector
comp <- c(1 + 1i, 1 + 2i, 1 + 3i)
comp
## [1] 1+1i 1+2i 1+3i
#Returns type of an object
typeof(comp)
## [1] "complex"
#Creates a raw vector
raw(3)
## [1] 00 00 00
#Returns type of the vector
typeof(raw(3))
## [1] "raw"
Exercise 5.2 (Vector of Cards)
#Creates a character vector
hand <- c("ace", "king", "queen", "jack", "ten")
hand
## [1] "ace" "king" "queen" "jack" "ten"
#Returns type of an object
typeof(hand)
## [1] "character"
5.4 Arrays
#Creates a 3-dimensional array
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
Exercise 5.3 (Make a Matrix)
#Creates a character vector
hand1 <- c("ace", "king", "queen", "jack", "ten", "spades", "spades",
"spades", "spades", "spades")
#Turns the character vector into a matrix via three different commands
matrix(hand1, nrow = 5)
## [,1] [,2]
## [1,] "ace" "spades"
## [2,] "king" "spades"
## [3,] "queen" "spades"
## [4,] "jack" "spades"
## [5,] "ten" "spades"
matrix(hand1, ncol = 2)
## [,1] [,2]
## [1,] "ace" "spades"
## [2,] "king" "spades"
## [3,] "queen" "spades"
## [4,] "jack" "spades"
## [5,] "ten" "spades"
dim(hand1) <- c(5, 2)
#Repeats the exercise above but in a different way
#Creates a character vector in a different order
hand2 <- c("ace", "spades", "king", "spades", "queen", "spades", "jack",
"spades", "ten", "spades")
#Turns the character vector into a matrix via two different commands
matrix(hand2, nrow = 5, byrow = TRUE)
## [,1] [,2]
## [1,] "ace" "spades"
## [2,] "king" "spades"
## [3,] "queen" "spades"
## [4,] "jack" "spades"
## [5,] "ten" "spades"
matrix(hand2, ncol = 2, byrow = TRUE)
## [,1] [,2]
## [1,] "ace" "spades"
## [2,] "king" "spades"
## [3,] "queen" "spades"
## [4,] "jack" "spades"
## [5,] "ten" "spades"
5.8 Data Frames
#Creates a data frame
df <- data.frame(face = c("ace", "two", "six"),
suit = c("clubs", "clubs", "clubs"), value = c(1, 2, 3))
df
## face suit value
## 1 ace clubs 1
## 2 two clubs 2
## 3 six clubs 3
#Gives names to a list
list(face = "ace", suit = "hearts", value = 1)
## $face
## [1] "ace"
##
## $suit
## [1] "hearts"
##
## $value
## [1] 1
#Gives names to a vector
c(face = "ace", suit = "hearts", value = "one")
## face suit value
## "ace" "hearts" "one"
#Returns type of "df"
typeof(df)
## [1] "list"
#Returns class of "df"
class(df)
## [1] "data.frame"
#Returns types of objects that are grouped together
str(df)
## 'data.frame': 3 obs. of 3 variables:
## $ face : chr "ace" "two" "six"
## $ suit : chr "clubs" "clubs" "clubs"
## $ value: num 1 2 3
#Prevents R from saving character strings as factors
df <- data.frame(face = c("ace", "two", "six"),
suit = c("clubs", "clubs", "clubs"), value = c(1, 2, 3),
stringsAsFactors = FALSE)
#Creates a data frame to build an entire deck of cards
deck <- data.frame(
face = c("king", "queen", "jack", "ten", "nine", "eight", "seven", "six",
"five", "four", "three", "two", "ace", "king", "queen", "jack", "ten",
"nine", "eight", "seven", "six", "five", "four", "three", "two", "ace",
"king", "queen", "jack", "ten", "nine", "eight", "seven", "six", "five",
"four", "three", "two", "ace", "king", "queen", "jack", "ten", "nine",
"eight", "seven", "six", "five", "four", "three", "two", "ace"),
suit = c("spades", "spades", "spades", "spades", "spades", "spades",
"spades", "spades", "spades", "spades", "spades", "spades", "spades",
"clubs", "clubs", "clubs", "clubs", "clubs", "clubs", "clubs", "clubs",
"clubs", "clubs", "clubs", "clubs", "clubs", "diamonds", "diamonds",
"diamonds", "diamonds", "diamonds", "diamonds", "diamonds", "diamonds",
"diamonds", "diamonds", "diamonds", "diamonds", "diamonds", "hearts",
"hearts", "hearts", "hearts", "hearts", "hearts", "hearts", "hearts",
"hearts", "hearts", "hearts", "hearts", "hearts"),
value = c(13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 13, 12, 11, 10, 9, 8,
7, 6, 5, 4, 3, 2, 1, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 13, 12, 11,
10, 9, 8, 7, 6, 5, 4, 3, 2, 1)
)
deck
## face suit value
## 1 king spades 13
## 2 queen spades 12
## 3 jack spades 11
## 4 ten spades 10
## 5 nine spades 9
## 6 eight spades 8
## 7 seven spades 7
## 8 six spades 6
## 9 five spades 5
## 10 four spades 4
## 11 three spades 3
## 12 two spades 2
## 13 ace spades 1
## 14 king clubs 13
## 15 queen clubs 12
## 16 jack clubs 11
## 17 ten clubs 10
## 18 nine clubs 9
## 19 eight clubs 8
## 20 seven clubs 7
## 21 six clubs 6
## 22 five clubs 5
## 23 four clubs 4
## 24 three clubs 3
## 25 two clubs 2
## 26 ace clubs 1
## 27 king diamonds 13
## 28 queen diamonds 12
## 29 jack diamonds 11
## 30 ten diamonds 10
## 31 nine diamonds 9
## 32 eight diamonds 8
## 33 seven diamonds 7
## 34 six diamonds 6
## 35 five diamonds 5
## 36 four diamonds 4
## 37 three diamonds 3
## 38 two diamonds 2
## 39 ace diamonds 1
## 40 king hearts 13
## 41 queen hearts 12
## 42 jack hearts 11
## 43 ten hearts 10
## 44 nine hearts 9
## 45 eight hearts 8
## 46 seven hearts 7
## 47 six hearts 6
## 48 five hearts 5
## 49 four hearts 4
## 50 three hearts 3
## 51 two hearts 2
## 52 ace hearts 1