[R] Odp: Grouping

Petr PIKAL petr.pikal at precheza.cz
Fri Jan 25 13:48:08 CET 2008


Hi


r-help-bounces at r-project.org napsal dne 24.01.2008 23:21:05:

> I need to group some subjects according to two
> variables (var1, var2)
> 
> I have a data frame that looks something like this:
> 
> Subject   var1   var2 
> 1         400    150 
> 2         320    100 
> 3         580    600 
> 4         145    789 
> 
> 
>   Each suject would fall into one of 2 categories ('A'
> or 'B'). I want to find (and see) all the possible
> combinations of letter assignment that exist, but
> every combination needs to have an equal amount of
> subjects categorized as 'A' or 'B'(eg. if I have 20
> subjects there should be 10 assigned with letter 'A'
> and 10 with letter 'B')
> 
> Thank you in advance for any hints,

The best hint seems to be trying to post some code which will explain more 
clearly what you want to achieve. You want to split your data to two 
groups according to the value of var1 and var2 and in the same time to 
keep both groups the same size? It seems to me rather contradictive.

If you want to sample your data without replacement into 2 groups with 
same number of members just sample a vector of indices

sel<-sample(1:20, 10)
x<-1:20
x[sel]
 [1] 20  3 13 12 14  7 11  1  5  2
x[-sel]
 [1]  4  6  8  9 10 15 16 17 18 19

Regards
Petr



> 
> Judith
> 
> 
> 
> 
____________________________________________________________________________________
> Never miss a thing.  Make Yahoo your home page.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list