1 Changing values in place

vec <- c(0,0,0,0,0,0)
vec
## [1] 0 0 0 0 0 0
vec[1] <- 100
vec
## [1] 100   0   0   0   0   0
vec[c(1,3,4)] <- c(1,1,1)
vec
## [1] 1 0 1 1 0 0
vec[4:6] <- vec[4:6] + 1
vec
## [1] 1 0 1 2 1 1
vec[7] <- 98
vec
## [1]  1  0  1  2  1  1 98
# loading the "dec" data set:
deck <- read.csv("deck.csv")

# creating a new data set:
deck2 <- deck

# adding and deleting columns to a data frame:
deck2$new <- 1:52
head(deck2)
##    face   suit value new
## 1  king spades    13   1
## 2 queen spades    12   2
## 3  jack spades    11   3
## 4   ten spades    10   4
## 5  nine spades     9   5
## 6 eight spades     8   6
deck2$new <- NULL
head(deck2)
##    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

In the game of war, aces have the highest value (14). Now I will assign this value to each of the aces of this deck.

# accessing the aces and their values:
deck2[c(13, 26, 39, 52), ]
##    face     suit value
## 13  ace   spades     1
## 26  ace    clubs     1
## 39  ace diamonds     1
## 52  ace   hearts     1
deck2[c(13, 26, 39, 52), 3]
## [1] 1 1 1 1
deck2$value[c(13, 26, 39, 52)]
## [1] 1 1 1 1
deck2$value[c(13, 26, 39, 52)] <- 14
deck2$value[c(13, 26, 39, 52)]
## [1] 14 14 14 14
# shuffling would make it hard to locate the aces in the deck. the way around is logical subsetting.

2 Logical Subsetting

Figure 1: R’s Logical Operators

Figure 1: R’s Logical Operators

Each one of the operators compare element-to-element except %in% operator, which tests whether the values in the left side are in the vector on the right side.

1 %in% c(3,4,5)
## [1] FALSE
c(1,2) %in% c(3,4,5)
## [1] FALSE FALSE
c(1,2,3,4) %in% c(3,4,5)
## [1] FALSE FALSE  TRUE  TRUE
# how many aces:
sum(deck2$face == "ace")
## [1] 4
# finding out aces and assigning them some value:
deck2$value[deck2$face == "ace"]
## [1] 14 14 14 14
deck2$value[deck2$face == "ace"] <- 14

# in "Heart", every card has a value of zero:
deck3 <- deck
deck3$value <- 0
head(deck3, 13)
##     face   suit value
## 1   king spades     0
## 2  queen spades     0
## 3   jack spades     0
## 4    ten spades     0
## 5   nine spades     0
## 6  eight spades     0
## 7  seven spades     0
## 8    six spades     0
## 9   five spades     0
## 10  four spades     0
## 11 three spades     0
## 12   two spades     0
## 13   ace spades     0
# except the suit of hearts has a value of 1:
deck3$value[deck3$suit == "hearts"] <- 1

In Hearts, the queen of spades has a value of 13. To assign this value, we need to use boolean operators.

2.1 Boolean Operators

Figure 2: R’s Boolean Operators

Figure 2: R’s Boolean Operators

# they also make element-to-element comparisons:
a <- c(1,2,3)
b <- c(1,2,4)
c <- c(1,2,3)
a == b & b == c
## [1]  TRUE  TRUE FALSE
# finding out the queen of spades and assigning a particular value:
deck$value[deck3$face == "queen" & deck3$suit == "spades"] <- 13

# or assign a name to the test:
queenOfSpades <- deck3$face == "queen" & deck3$suit == "spades"
deck3$value[queenOfSpades] <- 13
deck3[queenOfSpades, ]
##    face   suit value
## 2 queen spades    13

In blackjack, each number card has a value equal to its face value, each face card has a value of 10, and each ace has a value of 11 or 1, depending on the final results of the game.

deck4 <- deck
facecard <- deck4$face %in% c("king", "queen", "jack")
deck4$value[facecard] <- 10
deck4[facecard, ]
##     face     suit value
## 1   king   spades    10
## 2  queen   spades    10
## 3   jack   spades    10
## 14  king    clubs    10
## 15 queen    clubs    10
## 16  jack    clubs    10
## 27  king diamonds    10
## 28 queen diamonds    10
## 29  jack diamonds    10
## 40  king   hearts    10
## 41 queen   hearts    10
## 42  jack   hearts    10

We don’t have enough information to assign values to aces, as the ultimate value of aces depends on the final results of the game, which brings us to how to handle missing information in R.

3 Missing Information

1 + NA
## [1] NA
NA == 1
## [1] NA
# ignoring NA:
c(NA, 1:50)
##  [1] NA  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
## [26] 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
## [51] 50
mean(c(NA, 1:50))
## [1] NA
mean(c(NA, 1:50), na.rm = T)
## [1] 25.5
# counting how many missing values are in the data:
NA == NA
## [1] NA
c(1,3,4,NA) == NA
## [1] NA NA NA NA
is.na(NA)
## [1] TRUE
is.na(c(1,3,4,NA))
## [1] FALSE FALSE FALSE  TRUE
# setting the values of aces as NA:
deck4$value[deck4$face == "ace"] <- NA