How to find the number of changes when tossing a coin in R?


To find the number of changes when tossing a coin in R, we can follow the below steps −

  • First of all, create a vector using rbinom function.

  • Then, use rle function to find the table of changes.

  • After that, use length in the output of rle.

Example 1

Create the vector

Let’s create a vector as shown below −

x1<-rbinom(500,1,0.5)
x1

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

 [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

Find table of changes

Using rle function to find the changes in vector x1 −

x1<-rbinom(500,1,0.5)
rle(x1)

Output

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 ...

Find the number of changes

Using length function along with the output of rle to find the total number of changes in vector x1 −

x1<-rbinom(500,1,0.5)
length(rle(x1)$lengths)-1

Output

[1] 260

Example 2

Create the vector

Let’s create a vector as shown below −

x2<-rbinom(500,1,0.2)
x2

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

 [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

Find table of changes

Using rle function to find the changes in vector x2 −

x2<-rbinom(500,1,0.2)
rle(x2)

Output

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 ...

Find the number of changes

Using length function along with the output of rle to find the total number of changes in vector x2 −

x2<-rbinom(500,1,0.2)
length(rle(x2)$lengths)-1

Output

[1] 152

Example 3

Create the vector

Let’s create a vector as shown below −

x3<-rbinom(500,1,0.1)
x3

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

 [1]  0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 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om(500,1,0.1)
rle(x3)

Output

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 ...

Find the number of changes

Using length function along with the output of rle to find the total number of changes in vector x3 −

x3<-rbinom(500,1,0.1)
length(rle(x3)$lengths)-1

Output

[1] 79

Example 4

Create the vector

Let’s create a vector as shown below −

x4<-rbinom(500,1,0.9)
x4

Output

On executing, the above script generates the below output(this output will vary on your system due to randomization) −

 [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

Find table of changes

Using rle function to find the changes in vector x4 −

x4<-rbinom(500,1,0.9)
rle(x4)

Output

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 ...

Find the number of changes

Using length function along with the output of rle to find the total number of changes in vector x4 −

x4<-rbinom(500,1,0.9)
length(rle(x4)$lengths)-1

Output

[1] 98

Updated on: 08-Nov-2021

46 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements