在R中抛硬币时如何找到变化的数量?
要查找在R中掷硬币时的变化次数,我们可以按照以下步骤操作-
首先,使用rbinom函数创建一个向量。
然后,使用rle函数查找更改表。
之后,在rle的输出中使用length。
示例1
创建向量
让我们创建一个如下所示的向量-
x1<-rbinom(500,1,0.5) x1输出结果
执行时,上述脚本生成以下内容output(thisoutputwillvaryonyoursystemduetorandomization)-
[1] 1 1 0 0 0 1 1 0 1 0 1 0 0 0 1 1 1 1 0 0 1 1 0 1 0 0 1 0 1 1 1 0 0 0 1 1 0 [38] 1 1 1 1 0 1 0 0 0 0 0 0 0 1 1 0 1 1 0 1 0 0 1 0 0 0 0 1 0 0 1 1 1 1 0 1 0 [75] 0 1 0 1 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 1 0 1 0 0 0 0 1 [112] 1 1 1 0 0 0 0 0 0 0 1 1 0 1 1 1 0 1 1 1 0 1 1 1 0 0 1 0 1 1 0 0 1 1 1 1 1 [149] 1 1 0 1 0 0 0 1 1 0 0 1 1 0 1 0 0 0 0 1 0 1 1 0 0 0 0 1 0 0 0 1 1 1 1 1 1 [186] 0 0 1 1 0 0 0 1 1 1 1 0 1 0 0 0 1 1 1 0 1 0 1 0 1 1 1 1 1 1 1 0 0 0 1 0 1 [223] 1 1 1 0 0 1 0 0 0 0 0 0 0 0 1 1 1 1 1 0 1 1 1 0 0 1 0 0 1 0 1 0 0 1 1 0 1 [260] 1 0 1 0 0 1 1 1 0 0 1 1 1 1 1 1 1 1 0 1 0 0 1 0 1 0 0 1 0 0 1 1 1 0 1 0 1 [297] 1 0 0 0 0 1 0 1 1 1 0 0 0 0 1 1 1 1 1 0 1 0 0 0 1 1 0 0 1 1 1 1 1 0 1 0 0 [334] 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 1 [371] 0 1 0 1 1 1 1 1 0 1 0 1 0 0 0 1 1 1 1 0 1 1 1 1 0 0 1 0 0 0 0 0 0 0 1 1 1 [408] 0 0 0 1 1 1 0 0 0 0 1 1 0 0 1 0 1 0 1 0 0 0 1 0 1 1 0 1 0 0 1 0 0 1 1 0 1 [445] 0 1 1 1 1 1 0 1 1 1 0 1 1 0 1 1 1 1 1 1 0 0 0 0 1 0 0 0 1 0 1 0 1 1 0 1 0 [482] 1 0 1 0 1 1 0 0 1 1 0 0 0 1 0 0 1 0 0
查找更改表
使用rle函数查找向量x1的变化-
x1<-rbinom(500,1,0.5) rle(x1)输出结果
Run Length Encoding lengths: int [1:240] 2 3 2 1 1 1 1 3 4 2 ... values : int [1:240] 1 0 1 0 1 0 1 0 1 0 ...
查找更改次数
使用长度函数以及rle的输出来查找向量x1的总变化次数-
x1<-rbinom(500,1,0.5) length(rle(x1)$lengths)-1输出结果
[1] 260
示例2
创建向量
让我们创建一个如下所示的向量-
x2<-rbinom(500,1,0.2) x2输出结果
执行时,上述脚本生成以下内容output(thisoutputwillvaryonyoursystemduetorandomization)-
[1] 0 0 1 0 1 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 [38] 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 1 1 0 1 1 0 [75] 1 0 0 1 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 [112] 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 [149] 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 [186] 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 [223] 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 [260] 0 0 0 0 1 0 1 0 1 0 0 1 0 0 0 0 0 1 0 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 [297] 0 1 1 0 0 0 0 0 1 0 0 1 1 0 0 1 1 0 1 1 0 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 [334] 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 [371] 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [408] 0 1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 [445] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [482] 1 0 1 0 1 0 1 0 0 1 1 0 0 0 0 0 0 0 0
查找更改表
使用rle函数查找向量x2的变化-
x2<-rbinom(500,1,0.2) rle(x2)输出结果
Run Length Encoding lengths: int [1:163] 10 1 6 1 1 2 2 1 4 1 ... values : int [1:163] 0 1 0 1 0 1 0 1 0 1 ... Run Length Encoding lengths: int [1:147] 1 7 1 17 1 5 1 3 1 4 ... values : int [1:147] 1 0 1 0 1 0 1 0 1 0 ... Run Length Encoding lengths: int [1:171] 2 1 5 3 2 1 1 1 2 1 ... values : int [1:171] 0 1 0 1 0 1 0 1 0 1 ...
查找更改次数
使用长度函数以及rle的输出来查找向量x2的总变化次数-
x2<-rbinom(500,1,0.2) length(rle(x2)$lengths)-1输出结果
[1] 152
示例3
创建向量
让我们创建一个如下所示的向量-
x3<-rbinom(500,1,0.1) x3输出结果
执行时,上述脚本生成以下内容output(thisoutputwillvaryonyoursystemduetorandomization)-
[1] 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 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表
使用rle函数查找向量x3的变化-
x3<-rbinom(500,1,0.1) rle(x3)输出结果
Run Length Encoding lengths: int [1:77] 6 1 16 1 8 1 42 1 14 1 ... values : int [1:77] 0 1 0 1 0 1 0 1 0 1 ...
查找更改次数
使用长度函数以及rle的输出来查找向量x3的总变化次数-
x3<-rbinom(500,1,0.1) length(rle(x3)$lengths)-1输出结果
[1] 79
示例4
创建向量
让我们创建一个如下所示的向量-
x4<-rbinom(500,1,0.9) x4输出结果
执行时,上述脚本生成以下内容output(thisoutputwillvaryonyoursystemduetorandomization)-
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 [38] 1 1 1 0 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 [75] 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 [112] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [149] 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 1 1 1 1 1 0 1 1 [186] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 [223] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 [260] 1 1 1 1 0 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [297] 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [334] 0 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 1 0 1 [371] 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 [408] 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 [445] 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 [482] 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1
查找更改表
使用rle函数查找向量x4的变化-
x4<-rbinom(500,1,0.9) rle(x4)输出结果
Run Length Encoding lengths: int [1:91] 14 2 43 1 14 1 5 1 13 1 ... values : int [1:91] 1 0 1 0 1 0 1 0 1 0 ...
查找更改次数
使用长度函数以及rle的输出来查找向量x4中变化的总数-
x4<-rbinom(500,1,0.9) length(rle(x4)$lengths)-1输出结果
[1] 98