1 Selecting values

deck <- read.csv("deck.csv")

deck[1,1]
## [1] king
## Levels: ace eight five four jack king nine queen seven six ten three two
deck[1, 1:3]
##   face   suit value
## 1 king spades    13
# repetition:
deck[c(1,1), c(1,2,3)]
##     face   suit value
## 1   king spades    13
## 1.1 king spades    13
# to coerce R to create a data frame while selecting 1 column:
deck[1:2, 1:2]
##    face   suit
## 1  king spades
## 2 queen spades
deck[1:2, 1]
## [1] king  queen
## Levels: ace eight five four jack king nine queen seven six ten three two
deck[1:2, 1, drop = F]
##    face
## 1  king
## 2 queen
# using negative integers to subset:
deck[-(2:52), 1:3]
##   face   suit value
## 1 king spades    13
# useful to retain most of the data.

# using black space:
deck[1, ]
##   face   suit value
## 1 king spades    13
# logical subsetting:
deck[1, c(T, T, F)]
##   face   suit
## 1 king spades
# subsetting by names:
deck[1, c("face", "suit")]
##   face   suit
## 1 king spades
deck[ , "value"]
##  [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
## [26]  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
## [51]  2  1

2 Dealing a card

# making a function that returns the first row of "deck":
deal <- function(cards) {
  cards[1, ]
}
# but it returns the same values each time!

3 Shuffling the deck

# reordering rows:
deck2 <- deck[c(2,1,3:52), ]
head(deck2)
##    face   suit value
## 2 queen spades    12
## 1  king spades    13
## 3  jack spades    11
## 4   ten spades    10
## 5  nine spades     9
## 6 eight spades     8
# shuffling in a random order:
random <- sample(1:52, size = 52)
random
##  [1]  7 41 31 10 43 48 38 33  4 13 14 28  6 39 47 51 24 12 29  1 42 25 35  3 21
## [26] 16  8 34 32 46 11 36 44 15 49 37 20 50 45  5 18 26 19  9 17 23 52  2 27 40
## [51] 22 30
deck3 <- deck[random, ]
head(deck3)
##     face     suit value
## 7  seven   spades     7
## 41 queen   hearts    12
## 31  nine diamonds     9
## 10  four   spades     4
## 43   ten   hearts    10
## 48  five   hearts     5
# writing the "shuffle" function:
shuffle <- function(cards) {
  random <- sample(1:52, size = 52)
  cards[random, ]
}

deal(deck)
##   face   suit value
## 1 king spades    13
deck2 <- shuffle(deck)
deal(deck2)
##     face     suit value
## 37 three diamonds     3

4 Dollar signs and double brackets

Dollar signs are used to subset objects from data frames.

deck$value
##  [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
## [26]  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
## [51]  2  1
median(deck$value)
## [1] 7
# $ can also be used in named lists:
lst <- list(numbers = c(1,3), logical = T, strings = c("a", "c"))
lst
## $numbers
## [1] 1 3
## 
## $logical
## [1] TRUE
## 
## $strings
## [1] "a" "c"
lst[1]
## $numbers
## [1] 1 3
# it gives one smaller list with one element (the "numbers" vector). 

sum(lst[1])
## Error in sum(lst[1]): invalid 'type' (list) of argument
# it doesn't work because lst[1] is a list, not a vector.

lst$numbers
## [1] 1 3
sum(lst$numbers)
## [1] 4
# double brackets can also be used to subset the inner elements:
lst[[1]]
## [1] 1 3
Figure 1: Subsetting from a list

Figure 1: Subsetting from a list

Note: It’s not a good practice to use the function “attach” after loading a data set in R. ***