adschwab - 8 months ago 70

R Question

This question is in regards to R. I would like to generate a "non-random" sample of 1's and 0's where the next value generated is dependant on the previous value. For example, if the first value in the vector is 1 then there is a 60% chance that next value is 1, and if the next value is a 0, then there is a 60% chance that the following will be 0. I have attached the question as it was posed to me. I greatly appreciate any help.

Answer Source

Given your conditions, I would generate a sequence using `for`

loop.

```
set.seed(111)
n = 10000 #As the Q said at least 10,000
seq = vector()
seq[1] = sample(0:1, 1) #Also given in the Q
for(i in 2:n) {
if(seq[i-1] == 0){
seq[i] = sample(0:1, 1 , prob = c(0.6,0.4))
}
else{
seq[i] = sample(0:1, 1 , prob = c(0.4,0.6))
}
}
table(seq)
seq
0 1
4961 5039
```