Re: [R] drawing samples based on a matching variable

2010-09-29 Thread L Brown
Thanks much for all the help, R-helpers. Ended up getting the counts of the categories of the matching variable in both x and y and then limiting the sample from there. No longer really random, but I think it's fine for my purposes. Thanks again. LB On 28 September 2010 18:40, Michael Bedward wro

Re: [R] drawing samples based on a matching variable

2010-09-29 Thread Charles C. Berry
On Tue, 28 Sep 2010, L Brown wrote: Hi, everyone. I have what I hope will be a simple coding question. It seems this is a common job, but so far I've had trouble finding the answer in searches. I have two matrices (x and y) with a different number of observations in each. I need to draw a rando

Re: [R] drawing samples based on a matching variable

2010-09-28 Thread Michael Bedward
On 29 September 2010 09:47, Remko Duursma wrote: > > > How about: > > y[y[,2] %in% x.samp[,2],] > > gives you the subset of y where values in the second column are restricted > to your sample from x. > > You can then sample from this matrix, if you need to... Just saw this reply tangled up in anot

Re: [R] drawing samples based on a matching variable

2010-09-28 Thread Michael Bedward
Hello LB, It's one of those problems that's basic but tricky :) I don't have an elegant one-liner for it but here's a function that would do it... function(xs, y) { # sample matrix y such that col 2 of the sample matches # col 2 of matrix xs used <- logical(nrow(y)) yi <- integer(nrow(xs))

[R] drawing samples based on a matching variable

2010-09-28 Thread L Brown
Hi, everyone. I have what I hope will be a simple coding question. It seems this is a common job, but so far I've had trouble finding the answer in searches. I have two matrices (x and y) with a different number of observations in each. I need to draw a random sample without replacement of observa