0

I want a multinominal distributed data frame with dummies. The probabilities should be applied to the columns. I have following code which seems a bit awkward. Does anyone have a better idea?

set.seed(1234)
data.table::transpose(data.frame(rmultinom(10, 1, c(1:5)/5)))

#    V1 V2 V3 V4 V5
# 1   0  0  0  1  0
# 2   0  0  0  0  1
# 3   0  0  0  0  1
# 4   0  1  0  0  0
# 5   0  0  0  0  1
# 6   0  0  0  0  1
# 7   0  0  0  1  0
# 8   0  1  0  0  0
# 9   0  0  0  0  1
# 10  0  0  0  1  0
jay.sf
  • 60,139
  • 8
  • 53
  • 110

1 Answers1

0

A little shorter: and doesn't involve multiple coercions.

data.frame(t(rmultinom(10, 1, c(1:5)/5)))

or

library(data.table)
data.table(t(rmultinom(10, 1, c(1:5)/5)))
lmo
  • 37,904
  • 9
  • 56
  • 69