您如何使用 mapply 函数从数据集中创建随机抽取?
How do you using the mapply function to create random draws from a dataset?
我目前在数据集中列出了从 -10 亿到正 10 亿的随机串数,仅此而已。我想编写一个函数,以便它从数据集中提取 5 个随机数 100,000 次,然后查看该数字有多少次低于 0。我可以为此使用 mapply 函数吗?或者其他功能会更好。
数据集称为 numbers,因为它有 2 列,我想从名为 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))
}
}
```
有人能帮忙吗?
这是一个缩小尺寸的例子:
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
我目前在数据集中列出了从 -10 亿到正 10 亿的随机串数,仅此而已。我想编写一个函数,以便它从数据集中提取 5 个随机数 100,000 次,然后查看该数字有多少次低于 0。我可以为此使用 mapply 函数吗?或者其他功能会更好。
数据集称为 numbers,因为它有 2 列,我想从名为 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))
}
}
```
有人能帮忙吗?
这是一个缩小尺寸的例子:
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