'How to solving Markov Chain problem in R?
I want to solve this problem using R for my Computer Programming class;
''There are 100 people in a room. Everyone has $100. Randomly everyone gives 1 dollar to another person. What will be the income distribution when this is repeated 1 million times?''
I know I should use a loop because it will be repeated a lot, but I couldn't find how to write the other parts.I am a beginner in R program.Thanks for your answers
Solution 1:[1]
library(dplyr)
dfdf <- data.frame(
People = c(1:100),
Money = rep(100,100)
)
for (i in 1:1000000){
index <- which(dfdf$Money>0)
index_2 <-sample(index, length(index)/2)
dfdf[index_2, 2] <- dfdf[index_2, 2] - 1
dfdf[-index_2, 2] <- dfdf[-index_2, 2] + 1
}
hist(dfdf$Money)
Is this work for your study ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Esad |
