Creating a vector of random samples of variable length
I have an R script where
l2
is an array of all permutations of three letter combinations az ie the first values is aaa
and the last values is zzz
c <- rpois(n, 5)
is a vector of poisson distribution results.
I want to create a data frame that containtns a random number of samples from l2
and concatenates them into a path like this: abc -> vye -> tyb
for 3.
df_p <- data.frame('len' = c, 's1' = paste(sample(l2,c), collapse = ' -> '))
I've tried to use the above row to create it, but it returns the same path for every row. The length of this path corresponds to the first value in c.
Figured it out ...
unlist(lapply(c, function(x) paste(sample(l2,x), collapse = ' -> ') ))
... does the trick.
链接地址: http://www.djcxy.com/p/24880.html上一篇: 如何平均来自不同元素的分数
下一篇: 创建一个可变长度的随机样本矢量