Run Ctrl+Shift+Enter Insert Chunk Ctrl+Alt+I

deck <- read.csv("C:/0_RStudio/deck.csv",stringsAsFactors=FALSE)
head(deck)
deck[1, 1]
## [1] "king"
deck[1, c(1, 2, 3)]
new <- deck[1, c(1, 2, 3)]
  1. Repetition - ???
deck[c(1, 1), c(1, 2, 3)]
  1. Returns a Data Frame
deck[1:2, 1:2]
  1. Returns a vector
deck[1:2, 1]
## [1] "king"  "queen"
  1. Returns a data frame
deck[1:2, 1, drop = FALSE]
  1. Negative Numbers - ???
deck[-(2:52), 1:3]
  1. Illegal instruction - ???
#deck[c(-1, 1), 1]
  1. Zero - ??? The following instruction creates and empty object.
deck[0, 0]
  1. Blank Spaces - ???
deck[1, ]
  1. Logical Values - ???
deck[1, c(TRUE, TRUE, FALSE)]
  1. Check the attributes function
rows <- c(TRUE, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
F, F, F, F, F, F, F, F, F, F, F, F, F, F)
  1. Extract elements based on the name
deck[1, c("face", "suit", "value")]
  1. Use a blank space to tell R to extract every value in a dimension.
# the entire value column
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
  1. ??? Complete the following code to make a function that returns the first row of a data frame - ???:
deal <- function(cards) {
deck[1,1, "value"]
}
  1. Shuffle deck
deck2 <- deck[1:52, ]
head(deck2)
deck3 <- deck[c(2, 1, 3:52), ]
  1. How Modify Order
#First create a vector of 52 numbers in random order and store it in an object named random.
random <- sample(1:52, size = 52)
random
##  [1]  2 48 33 37  8 38  1 21 31 36 27 49 47  3 32 41 45 22 42 24 19 30 17 34 23
## [26] 39 28 44 18 12 16 51 15 43 40 25 11  9  4 20 14  6 10 50 29 13 52 26 46  5
## [51]  7 35
deck4 <- deck[random, ]
head(deck4)
  1. ??? Exercise. Use the preceding ideas to write a shuffle function. shuffle should take a data frame and return a shuffled copy of the data frame.

#shuffle <- function(cards) { #random <- sample(1:52, size = 52) #cards[random, ] #} #deal(deck) ## face suit value ## king spades 13 #deck2 <- shuffle(deck) #deal(deck2) ## face suit value ## jack clubs 11 #```

  1. Dollar Signs and Double Brackets. You can extract values from data frames and lists with the $ syntax. To select a column from a data frame, write the data frame’s name and the column name separated by a $. Notice that no quotes should go around the column name:
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
mean(deck$value)
## [1] 7
median(deck$value)
## [1] 7
  1. make a list: - ???
lst <- list(numbers = c(1, 2), logical = TRUE, strings = c("a", "b", "c"))
lst
## $numbers
## [1] 1 2
## 
## $logical
## [1] TRUE
## 
## $strings
## [1] "a" "b" "c"
  1. And then subset it - ???
lst[1]
## $numbers
## [1] 1 2

??? The result is a smaller list with one element. That element is the vector c(1, 2). This can be annoying because many R functions do not work with lists. For example, sum(lst[1]) will return an error. It would be horrible if once you stored a vector in a list, you could only ever get it back as a list: - ???

When you use the $ notation, R will return the selected values as they are, with no list
structure around them:


```r
lst$numbers
## [1] 1 2
## 1 2

You can then immediately feed the results to a function:

sum(lst$numbers)
## [1] 3
## 3

If the elements in your list do not have names (or you do not wish to use the names), you can use two brackets, instead of one, to subset the list. This notation will do the same thing as the $ notation:

lst[[1]]
## [1] 1 2
## 1 2

In other words, if you subset a list with single-bracket notation, R will return a smaller list. If you subset a list with double-bracket notation, R will return just the values that were inside an element of the list. You can combine this feature with any of R’s indexing methods:

lst["numbers"]
## $numbers
## [1] 1 2
lst[["numbers"]]
## [1] 1 2

#Changing Values in Place

vec <- c(0, 0, 0, 0, 0, 0)
vec
## [1] 0 0 0 0 0 0
vec[1] <- 1000
vec
## [1] 1000    0    0    0    0    0

Changing more than one value at the same time.

vec[c(1, 3, 5)] <- c(7, 9, 8)
vec
## [1] 7 0 9 0 8 0
vec[4:6] <- vec[4:6] + 1
vec
## [1] 7 0 9 1 9 1

Force the vector to grow.

vec[7] <- 0
vec
## [1] 7 0 9 1 9 1 0
deck2 <- read.csv("C:/0_RStudio/deck.csv",stringsAsFactors=FALSE)
deck2$new <- 1:52
head(deck2)

Remove a column from a dataframe.

deck2$new <- NULL
head(deck2)
deck2[c(13, 26, 39, 52), ]
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)] <- c(14, 14, 14, 14)