###Chapt 5 Exercises

Section 5.1 Atomic Vectors

die <- c(1, 2, 3, 4, 5, 6)
die
## [1] 1 2 3 4 5 6
is.vector(die) #tests whether an object is an atomic vector
## [1] TRUE
five <- 5
five
## [1] 5
is.vector(five) #atomic vector with just one value
## [1] TRUE
length(five) #R saves single values as an atomic vector of length 1
## [1] 1
length(die) #length returns the length of an atomic vector
## [1] 6
int <- 1L #creates an integer vector
text <- "ace" #creates a character vector
int <- c(1L, 5L) #combine an element with the c function
text <- c("ace", "hearts")

Section 5.1.1 Atomic Vectors (Doubles)

die <- c(1, 2, 3, 4, 5, 6) #R will save any number that you type in R as a double
die
## [1] 1 2 3 4 5 6
typeof(die) #you can also ask R what type of object an object is
## [1] "double"

Section 5.1.2 Atomic Vectors (Integers)

int <- c(-1L, 2L, 4L) #create an integer in R by typing a number followed by an uppercase L
int
## [1] -1  2  4
typeof(int) #R won’t save a number as an integer unless you include the L
## [1] "integer"
sqrt(2)^2 - 2 #floating-point errors result because each double is accurate to about 16 significant digits
## [1] 4.440892e-16

Section 5.1.3 Atomic Vectors (Characters)

text <- c("Hello",  "World") #create a character vector in R by typing a character or string of characters surrounded by quotes
text
## [1] "Hello" "World"
typeof(text)
## [1] "character"
typeof("Hello") #individual elements of a character vector are known as strings
## [1] "character"

Section 5.1.4 Atomic Vectors (Logicals)

3 > 4 #Logical vectors store TRUEs and FALSEs, R’s form of Boolean data
## [1] FALSE
logic <- c(TRUE, FALSE, TRUE) #type TRUE or FALSE in capital letters (without quotation marks), R will treat your input as logical data
logic
## [1]  TRUE FALSE  TRUE
typeof(logic)
## [1] "logical"
typeof(F)
## [1] "logical"

Section 5.1.5 Atomic Vectors (Complex and Raw)

comp <- c(1 + 1i, 1 + 2i, 1 + 3i) #R also recognizes two more types: complex and raw using i. It is doubtful that you will ever use these to analyze data.
comp
## [1] 1+1i 1+2i 1+3i
typeof(comp)
## [1] "complex"
raw(3) #Raw vectors store raw bytes of data
## [1] 00 00 00
typeof(raw(3))
## [1] "raw"

Section 5.3 Matrices

m <- matrix(die, nrow = 2) #organize your vector of values into a matrix with the specified number of rows using nrow - use ncol to specify number of columns
m
##      [,1] [,2] [,3]
## [1,]    1    3    5
## [2,]    2    4    6
m <- matrix(die, nrow = 2, byrow = TRUE) #you can fill the matrix row by row if you include the argument byrow = TRUE
m
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6

Section 5.4 Arrays

ar <- array(c(11:14, 21:24, 31:34), dim = c(2, 2, 3)) #function creates an n-dimensional array
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 (After Arrays)

hand1 <- c("ace", "king", "queen", "jack", "ten", "spades", "spades", "spades", "spades", "spades") #start by making a character vector with 10 values to build a matrix
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)

hand2 <- c("ace", "spades", "king", "spades", "queen", "spades", "jack", "spades", "ten", "spades")
matrix(hand2, nrow = 5, byrow = TRUE) #you can fill the matrix row by row if you include the argument 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"

Section 5.7 Lists

list1 <- list(100:130, "R", list(TRUE, FALSE)) #lists group together R objects, one dimensional set
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
card <- list("ace", "hearts", 1) #Use a list to store a single playing card
card
## [[1]]
## [1] "ace"
## 
## [[2]]
## [1] "hearts"
## 
## [[3]]
## [1] 1

Exercise 5.5 (After Lists)

card <- list("ace", "hearts", 1)
card
## [[1]]
## [1] "ace"
## 
## [[2]]
## [1] "hearts"
## 
## [[3]]
## [1] 1

Section 5.8 Data Frames

df <- data.frame(face = c("ace", "two", "six"), suit = c("clubs", "clubs", "clubs"), value = c(1, 2, 3)) #Data frames group vectors together into a two-dimensional table
df
##   face  suit value
## 1  ace clubs     1
## 2  two clubs     2
## 3  six clubs     3
typeof(df) #data.frame will turn each vector into a column of the new data fram
## [1] "list"
class(df) #you can prevent this behavior, factors as strings) by adding the argument stringsAsFactors = FALSE to data.frame
## [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
df <- data.frame(face = c("ace", "two", "six"), suit = c("clubs", "clubs", "clubs"), value = c(1, 2, 3), stringsAsFactors = FALSE)
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)) #You should avoid typing large data sets in by hand whenever possible. Typing invites typos and errors, not to mention RSI. It is always better to acquire large data sets as a computer file. You can then ask R to read the file and store the contents as an object.