# 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)

## 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 0 0 0 0 1 0 0
[38]  0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
[75]  0 0 0 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 0 0 0 0
[112] 0 0 0 0 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 1 0 0 0 0 0 0
[149] 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 0 1 1 0
[186] 1 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
[223] 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0
[260] 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
[297] 1 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0
[334] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0
[371] 0 0 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 1 0 0 0 0 0 0 0
[408] 0 0 0 0 0 0 1 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
[445] 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
[482] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Find table of changes

Using rle function to find the changes in vector x3 −

x3<-rbinom(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)

## Output

[1] 98