-1

This is what I am doing to tabulate the sampling results:

> n <- 1000
> tabulate(sample.int(4, n, replace = TRUE))
[1] 238 255 247 260

I will need to do this millions of time with different values of n. I don't care about the actual sequence of the sampling. All I need is the count in each bin.

Is there a built-in function that does this directly? It feels inefficient to do it in the way shown above.

Dong
  • 481
  • 4
  • 15
  • 1
    You mean `sapply(seq(1000), function(i)sample.int(4, i, replace = TRUE))` – Sotos Dec 18 '19 at 07:15
  • 1
    I don't think so. An alternative is to use `table`, `table(sample(4, n, replace = TRUE))` – Ronak Shah Dec 18 '19 at 07:24
  • @RonakShah You mean`table(sample(n, 4, replace = TRUE))`. – cimentadaj Dec 18 '19 at 07:36
  • thanks. I edited the question. I am just curious if there is a probability/statistical function does this directly. – Dong Dec 18 '19 at 07:56
  • please help to improve the question instead of simply downgrade. What I have is working, I am asking is if there is built-in functions that do this directly. – Dong Dec 18 '19 at 23:23

1 Answers1

0

I am not sure if the data frame df is something you are looking for

iter <- 20
n <- 1000
df <- as.data.frame(t(replicate(iter,table(sample.int(4, n, replace = TRUE)))))

such that

> df
     1   2   3   4
1  226 257 250 267
2  257 239 245 259
3  236 266 233 265
4  219 253 257 271
5  251 261 228 260
6  243 252 262 243
7  253 257 267 223
8  252 223 269 256
9  239 242 256 263
10 253 275 219 253
11 260 242 258 240
12 259 263 246 232
13 259 274 221 246
14 251 261 258 230
15 256 268 249 227
16 241 244 251 264
17 253 233 258 256
18 235 253 257 255
19 232 260 277 231
20 243 252 240 265
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81