thmsfuller066 at gmail.com
2010-May-22 23:06 UTC
[R] How sample without replacement on more than one variables?
Hello All, sample() only sample on one variable x. But I'm interested in sampling more than one variable without replacement. Suppose I have 3 vectors x, y, z. I want to draw samples from all three vectors such that the combination of the three elements in each draw is not the same as any previous draws. I could use expand.grid to generate a vector out of the three vectors. But when the number of vectors are large and the number of elements in some vectors are large, it will be infeasible to do so. If you know there is a method on sampling on more than one variables, would you please let me know? Thank you! -- Tom
Erik Iverson
2010-May-23 07:33 UTC
[R] How sample without replacement on more than one variables?
thmsfuller066 at gmail.com wrote:> Hello All, > > sample() only sample on one variable x. But I'm interested in sampling > more than one variable without replacement. > > Suppose I have 3 vectors x, y, z. I want to draw samples from all > three vectors such that the combination of the three elements in each > draw is not the same as any previous draws. I could use expand.grid to > generate a vector out of the three vectors. But when the number of > vectors are large and the number of elements in some vectors are > large, it will be infeasible to do so. > > If you know there is a method on sampling on more than one variables, > would you please let me know? Thank you! >Can you give a reproducible example? Since you suggested the method that is most reasonable, but it will not work in large cases, I suppose you'll have to draw independently from each vector one at a time, then somehow concatenate the results, perhaps as a character vector, even if the vectors are, say, integers. Then repeat this process checking each time if your new vector is %in% the vector. There may be a much better way, too, see if anyone else responds. Also, you'll have to think about what a unique sample is. If x <- 1:3 y <- 2:4 , is x = 2, y = 3 the same as x = 3, y = 2? Good luck, Erik
dusadrian
2010-May-23 07:56 UTC
[R] How sample without replacement on more than one variables?
This might help, depending on your exact needs:> v1 <- sample(letters[1:2], 10, replace=TRUE) > v2 <- sample(letters[3:4], 10, replace=TRUE) > v3 <- sample(letters[5:6], 10, replace=TRUE) > aa <- data.frame(v1=v1, v2=v2, v3=v3) > aav1 v2 v3 1 a d e 2 a d e 3 a c e 4 b d e 5 b d f 6 a c f 7 a c f 8 a c f 9 a c e 10 b c e> bb <- unique(aa) > bbv1 v2 v3 1 a d e 3 a c e 4 b d e 5 b d f 6 a c f 10 b c e You can sample from the "bb" dataframe, or from the corresponding rows of the "aa" dataframe that are unique (1, 3, 4, 5, 6 and 10) which can be obtained via rownames(bb). Hth, Adrian -- View this message in context: http://r.789695.n4.nabble.com/How-sample-without-replacement-on-more-than-one-variables-tp2227665p2227683.html Sent from the R help mailing list archive at Nabble.com.
Bernardo Rangel Tura
2010-May-23 10:47 UTC
[R] How sample without replacement on more than one variables?
On Sun, 2010-05-23 at 00:56 -0700, dusadrian wrote:> This might help, depending on your exact needs: > > v1 <- sample(letters[1:2], 10, replace=TRUE) > > v2 <- sample(letters[3:4], 10, replace=TRUE) > > v3 <- sample(letters[5:6], 10, replace=TRUE) > > aa <- data.frame(v1=v1, v2=v2, v3=v3)And now is simple, sample the row of data frame aa[sample(1:nrows(aa),3),] -- Bernardo Rangel Tura, M.D,MPH,Ph.D National Institute of Cardiology Brazil
Reasonably Related Threads
- Sample size estimation for non-inferiority log-rank and Wilcoxon rank-sum tests
- How not to print '\\' as '\\'
- How to get the names of all the packages that depend on a give package?
- enclosing with() in a function
- Weired problem when passing arguments using ...?