Simulate a random walk with purrr in R
问题 Can purrr simulate a random walk in fewer lines of code than base R? For example what would the following look like in purrr ? n <- 5 x <- numeric(n) for(i in 2:n){ x[i] <- x[i-1] + rnorm(1) } Edit: @Imo had exactly what I was looking for. To take this one step further I am assuming a random walk can be created and plotted with the following code tibble(x = 1:1000, y = cumsum(rnorm(1000, mean = 0))) %>% ggplot(aes(x=x,y=y))+ geom_point()+ geom_line() 来源: https://stackoverflow.com/questions