6. R Notation -R符号系统 学习

上一节中,我们创建了一副扑克牌:

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))
head(deck)
##    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

现在需要进行发牌和洗牌。

6.1Selecting Values(发一张牌)

R的符号系统,允许我们使用数据框的名称,后跟一对硬括号:来提取数据框中的值:

deck[ , ]

R 将使用第一个索引对数据帧的行进行子集化,并使用第二个索引对列进行子集化。

可以使用以下方式创建索引:

  • Positive integers

  • Negative integers

  • Zero

  • Blank spaces

  • Logical values

  • Names

6.1.1正整数索引

R Notion使用线性代数的ij表示法,假设我们要提取数据框第一行的值,可进行如下操作:

deck[1,]
##   face   suit value
## 1 king spades    13
deck[1,c(1,2,3)]
##   face   suit value
## 1 king spades    13
deck[1,1:3]
##   face   suit value
## 1 king spades    13
# 三种形式均可

可在deck[] 函数添加drop=false参数来返回单列数据,否则在提取单列数据时R会以向量的形式返回。

deck[,1]
##  [1] "king"  "queen" "jack"  "ten"   "nine"  "eight" "seven" "six"   "five" 
## [10] "four"  "three" "two"   "ace"   "king"  "queen" "jack"  "ten"   "nine" 
## [19] "eight" "seven" "six"   "five"  "four"  "three" "two"   "ace"   "king" 
## [28] "queen" "jack"  "ten"   "nine"  "eight" "seven" "six"   "five"  "four" 
## [37] "three" "two"   "ace"   "king"  "queen" "jack"  "ten"   "nine"  "eight"
## [46] "seven" "six"   "five"  "four"  "three" "two"   "ace"
deck[,1,drop=FALSE]
##     face
## 1   king
## 2  queen
## 3   jack
## 4    ten
## 5   nine
## 6  eight
## 7  seven
## 8    six
## 9   five
## 10  four
## 11 three
## 12   two
## 13   ace
## 14  king
## 15 queen
## 16  jack
## 17   ten
## 18  nine
## 19 eight
## 20 seven
## 21   six
## 22  five
## 23  four
## 24 three
## 25   two
## 26   ace
## 27  king
## 28 queen
## 29  jack
## 30   ten
## 31  nine
## 32 eight
## 33 seven
## 34   six
## 35  five
## 36  four
## 37 three
## 38   two
## 39   ace
## 40  king
## 41 queen
## 42  jack
## 43   ten
## 44  nine
## 45 eight
## 46 seven
## 47   six
## 48  five
## 49  four
## 50 three
## 51   two
## 52   ace

6.1.2负整数索引

索引时负整数的作用与正整数完全相反。R 将返回除负索引中的元素之外的所有元素。

deck[-(2:52), 1:3] ## face   suit value ## king spades    13
##   face   suit value
## 1 king spades    13

6.1.3 Zero

deck[0, 0] ## data frame with 0 columns and 0 rows,返回空的对象
## data frame with 0 columns and 0 rows

6.1.4 Blank Spaces

使用空格告诉 R 提取维度中的每个值

deck[1, ] 
##   face   suit value
## 1 king spades    13

6.1.5 Logical Values 逻辑值

deck[1, c(TRUE, TRUE, FALSE)]
##   face   suit
## 1 king spades

R 将返回对应于 a 的每一行TRUE对应的元素。

6.1.6 Names

按数据框中列的名称返回元素,可用于提取自己需要的变量数据:

deck[1, c("face", "suit", "value")]
##   face   suit value
## 1 king spades    13

6.2 发牌

建立一个每次发牌组中第一张牌的函数:

deal = function(deck){
  deck[1,]
}
deal(deck)
##   face   suit value
## 1 king spades    13

6.3 Shuffle the Deck 洗牌

此处,可以使用sample()函数实现对每行数据随机排列:

random = sample(1:52,size = 52)#利用不放回抽样生成随机排列
random
##  [1] 24  6 23 16 34 27 50 15 14  4  2 29 10 42 25 46 49  5 31 33 28 11 21 38  7
## [26]  9 22 30 18  1 13 12 35 43  3  8 44 32 17 51 41 40 36 48 26 20 19 45 39 52
## [51] 37 47
deck_r = deck[random,]#实现"洗牌"
deck_r
##     face     suit value
## 24 three    clubs     3
## 6  eight   spades     8
## 23  four    clubs     4
## 16  jack    clubs    11
## 34   six diamonds     6
## 27  king diamonds    13
## 50 three   hearts     3
## 15 queen    clubs    12
## 14  king    clubs    13
## 4    ten   spades    10
## 2  queen   spades    12
## 29  jack diamonds    11
## 10  four   spades     4
## 42  jack   hearts    11
## 25   two    clubs     2
## 46 seven   hearts     7
## 49  four   hearts     4
## 5   nine   spades     9
## 31  nine diamonds     9
## 33 seven diamonds     7
## 28 queen diamonds    12
## 11 three   spades     3
## 21   six    clubs     6
## 38   two diamonds     2
## 7  seven   spades     7
## 9   five   spades     5
## 22  five    clubs     5
## 30   ten diamonds    10
## 18  nine    clubs     9
## 1   king   spades    13
## 13   ace   spades     1
## 12   two   spades     2
## 35  five diamonds     5
## 43   ten   hearts    10
## 3   jack   spades    11
## 8    six   spades     6
## 44  nine   hearts     9
## 32 eight diamonds     8
## 17   ten    clubs    10
## 51   two   hearts     2
## 41 queen   hearts    12
## 40  king   hearts    13
## 36  four diamonds     4
## 48  five   hearts     5
## 26   ace    clubs     1
## 20 seven    clubs     7
## 19 eight    clubs     8
## 45 eight   hearts     8
## 39   ace diamonds     1
## 52   ace   hearts     1
## 37 three diamonds     3
## 47   six   hearts     6

把上述操作包装成一个洗牌函数:

shuffle = function(deck){
  random = sample(1:52,size = 52)
  deck_r = deck[random,]
}
deck_r1=shuffle(deck)
deal(deck_r1)#实现每洗一次牌,然后发一张牌
##    face     suit value
## 39  ace diamonds     1

把以上步骤总和起来,实现每洗一次牌同时发一张牌。

6.4美元符号和双括号

可以使用$语法从数据框和列表中提取值。(通常用于提取数据集中的变量)

mean(deck$value) #提取deck数据集中的变量value并计算均值
## [1] 7

双括号:

如果把列表想象成装载有货物的火车,那么单层括号[]返回某一节车厢,双层括号[[]]返回该车厢内的"货物"。例如:

lst <- list(numbers = c(1, 2), logical = TRUE, strings = c("a", "b", "c"))
lst[1]#单层括号
## $numbers
## [1] 1 2
lst[[1]]
## [1] 1 2