0

I basically want to create a vector which length is 82. This should contain either "W" or "L", but their occurrences should be randomly determined. I tried with this season<-rep(c("W","L"), times = 1, length.out = 82, each = 1) but I only get this: [1] "W" "L" "W" "L" "W" "L"... Trivial question but I am a newbie to R. Thanks!

FaCoffee
  • 7,609
  • 28
  • 99
  • 174

2 Answers2

4

You can do:

set.seed(1)
sample(c("W", "L"), 82, replace = TRUE)
# [1] "W" "W" "L" "L" "W" "L" ...

With set.seed you set the seed of the random generator and hence get the same results, whenever you run this 2 lines. If you run a second time sample you will get a different result.

thothal
  • 16,690
  • 3
  • 36
  • 71
  • Why choosing one specific seed instead of another? I know `set.seed()` makes random selections reproducible, but what drives the choice of a particular seed? – FaCoffee Aug 07 '15 at 11:07
  • Well, any number would do. It is just to get reproducible results. So whether you do `set.seed(1)` or `set.seed(2876632816)` up to you – thothal Aug 07 '15 at 11:09
  • As @thothal said, nothing, really. Just researcher choice. Read `?set.seed` for some serious details on the random number generation capabilities (it suggests using some seeds _might_ change the RNG seed for some underlying algos, but it's _highly_ unlikely). – hrbrmstr Aug 07 '15 at 11:11
  • Thanks guys. Appreciate your efforts. – FaCoffee Aug 07 '15 at 11:13
2

Another approach would be to control the underneath function that generates the data (because of hypothesis that are made about the data for example). Here is an example, suppose that you have to generates a random sequences of W and L drown by a Binomial (which is the only one that makes sense for this kind of simulation as pointed out by @Gregor).

just use the or rbinom for binomial and and then convert the output as a factor with labels to W and L.

Here is an example with the rbinom function that generates a sequence of 0 and 1 like a coin flip (both with 50% or probabilities) and then you add the labels you want (in this case W and L).

set.seed(123)
x <- rbinom(82, 1, 0.5)
x <- factor(x, labels = c("W", "L"))
x
 [1] W L W L L W L L L W L W L L W L W W W L L L L L L L L L W W L L L L
[35] W W L W W W W W W W W W W W W L W W L W L W W L L W L W W W L W L L
[69] L W L L L W W W W L W W W L
Levels: W L
SabDeM
  • 7,050
  • 2
  • 25
  • 38
  • @Gregor yes I was thinking about that. I am a little bit rusty in statistics. The only two useful functions (in this case with two values) I think are the `rbinom` and `runif`. Anyway I'll fix my answer, but I think that my code is valid, isn't it? – SabDeM Aug 07 '15 at 18:44
  • Yes, the code block is good. `runif` could be `round()`ed to mimic the results of `rbinom`---the results would not be different, it's just a slightly longer way of getting there. Unrounded `runif` is uniform, not normal. Normal and uniform are both continuous distributions, and are meaningless when it comes to the discrete simulation OP is interested in. – Gregor Thomas Aug 07 '15 at 18:57
  • I think you should only mention the binomial distribution. – Gregor Thomas Aug 07 '15 at 18:58
  • Thank you @Gregor... my statistics prof could be ashamed of myself – SabDeM Aug 07 '15 at 19:00