the problem when I want to use rep in R

If I want to make a sentence in R repeat 200 times
(sample(x=1:49,size=6,replace = FALSE)
but each group and each number it comes out can't be the same
Why cam't I write rep(sample(x=1:49,size=6,replace = FALSE),3,replace(FALSE))

G'day mate

Sorry, I cannot check the code right now, but I recon that it happen because each repetition has a different seed?

I would first make the sample and them repeat it... something like

a <- sample(...)
b <- rep(a,....)

Hope it helps

cheers
Fer

2 Likes

Hello! I use you way to write
a <- sample(x=1:49,size=6,replace = FALSE)
b <- rep(a,3)
b
it comes out
[1] 38 27 42 28 48 26 38 27 42 28 48 26 38 27 42 28 48 26
but actually I want each three groups have all different number
do you know how to reach the destination?
Thanks!

Not sure about others, but for me it's a little difficult to follow what's your expected result. And that's more so as you are using sample without set.seed.

Let me give you 3 examples of rep, and you can probably figure out what you want from them.

> rep(x=c(10, 20, 30), times=4)
 [1] 10 20 30 10 20 30 10 20 30 10 20 30
> rep(x=c(10, 20, 30), each=5)
 [1] 10 10 10 10 10 20 20 20 20 20 30 30 30 30 30
> rep(x=c(10, 20, 30), times=2, each=3)
 [1] 10 10 10 20 20 20 30 30 30 10 10 10 20 20 20 30 30 30

You can also take a look at ?rep, ?seq, ?sequence etc. There are plenty of variations.


Or may be you want replicate?

> set.seed(seed=82733)
> replicate(n=4, expr=sample(x=1:30, size=6, replace=FALSE))
     [,1] [,2] [,3] [,4]
[1,]   30    2    4    4
[2,]   10   18   21   28
[3,]   15   27   20   25
[4,]    9   20    5   27
[5,]    8   25   27   26
[6,]    4   19   22    6
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.