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)]
deck[c(1, 1), c(1, 2, 3)]
deck[1:2, 1:2]
deck[1:2, 1]
## [1] "king" "queen"
deck[1:2, 1, drop = FALSE]
deck[-(2:52), 1:3]
#deck[c(-1, 1), 1]
deck[0, 0]
deck[1, ]
deck[1, c(TRUE, TRUE, FALSE)]
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)
deck[1, c("face", "suit", "value")]
# 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
deal <- function(cards) {
deck[1,1, "value"]
}
deck2 <- deck[1:52, ]
head(deck2)
deck3 <- deck[c(2, 1, 3:52), ]
#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)
#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 #```
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
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"
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)