Reputation: 73
I currently have a random bunch numbers listed from -1billion to positive 1 billion in a dataset, that is all. I want to write a function so that it will pull 5 random numbers 100,000 times from the dataset and then see how many times the number is below 0. Can I use the mapply function for this? or would another function be better.
The dataset is called numbers as has 2 columns, with the column i want to pull the numbers from called listofnumbers.
Currently i have a for loop but it seems to take forever to run, the code is below
```
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))
}
}
```
Can anyone help?
Upvotes: 0
Views: 37
Reputation: 2301
Here is a reduced size example:
r <- -100:100
n <- 10
collect <- 1000
output <- replicate(collect, sum(sample(r, n) < 0))
hist(output)
You would simply replace r
, n
and collect
with your values. I.e., r = numbers$listofnumbers
, n = 5, collect = 100000
Upvotes: 2