r - Creating a sequence bases on value of previous element -


i'm trying 20 sequences of length 1000, each generated randomly adding 1 or 0 previous element in sequence.

i tried this:

h <- replicate(20, rep(0, 1000), simplify=false) lapply(h, function(v) for(i in 2:1000){ v[i] = v[i - 1] + 1 }) 

but loop in lapply doesn't appear anything, , i'm left list of 20 lists of 1000 0s.

what working way this? if there's way without loop, better :)

thanks

set.seed(100) x <- replicate(20, cumsum(sample(0:1, 1000, replace = true)), simplify = false) str(x) # list of 20  # $ : int [1:1000] 0 0 1 1 1 1 2 2 3 3 ...  # $ : int [1:1000] 0 0 1 2 2 2 2 2 3 4 ...  # $ : int [1:1000] 1 2 3 3 4 5 6 6 7 7 ...  # $ : int [1:1000] 0 0 1 1 2 2 3 4 4 5 ...  # $ : int [1:1000] 1 1 1 2 2 2 2 3 3 3 ...  # $ : int [1:1000] 1 2 2 3 4 5 6 7 7 7 ...  # $ : int [1:1000] 0 0 0 1 2 3 3 4 5 6 ...  # $ : int [1:1000] 1 1 1 2 3 3 4 4 4 5 ...  # $ : int [1:1000] 1 1 1 1 1 1 1 1 1 2 ...  # $ : int [1:1000] 1 1 2 3 3 3 3 3 3 3 ...  # $ : int [1:1000] 1 1 1 1 2 3 3 4 4 5 ...  # $ : int [1:1000] 0 0 1 1 1 2 3 3 3 4 ...  # $ : int [1:1000] 0 0 1 1 1 2 2 3 4 4 ...  # $ : int [1:1000] 0 0 1 1 2 2 3 4 4 4 ...  # $ : int [1:1000] 0 0 1 2 2 2 2 3 3 3 ...  # $ : int [1:1000] 1 2 3 3 3 4 5 6 6 7 ...  # $ : int [1:1000] 0 0 1 2 2 2 3 3 3 4 ...  # $ : int [1:1000] 1 1 2 3 3 3 3 3 4 4 ...  # $ : int [1:1000] 0 1 1 2 3 3 4 4 4 5 ...  # $ : int [1:1000] 0 0 1 2 2 3 3 4 4 5 ... 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -