我目前在数据集中列出了一个从-10亿到正10亿的随机串数字,仅此而已。我想写一个函数,让它从数据集中随机抽取5个随机数100,000次,然后看看有多少次这个数字小于0。我能为此使用mapply函数吗?或者另一个函数会更好。
数据集被称为numbers,因为它有两列,其中一列是我想要从名为listofnumbers中提取数字的列。
目前我有一个for循环,但它似乎需要永远运行,代码如下
```
n=5
m=100000
base_table <- as_tibble (0)
for (j in 1:m)
{
for (i in 1:n)
{
base_table[i,j] <- as_tibble(sample_n(numbers, 1) %>% pull(listofnumbers))
}
}
```有人能帮上忙吗?
发布于 2021-04-13 23:50:20
以下是一个缩小大小的示例:
r <- -100:100
n <- 10
collect <- 1000
output <- replicate(collect, sum(sample(r, n) < 0))
hist(output)您只需将r、n和collect替换为您的值。即,r= numbers$listofnumbers,n= 5,collect = 100000
https://stackoverflow.com/questions/67078057
复制相似问题