Try this:

A <- data.frame(V1 = rnorm(100), V2 = runif(100), V3 = rep(c(1,2,3,4,5),20))
do.call(cbind, lapply(aggregate(. ~ V3, A, FUN = sample, size = 5), c))

On Wed, Oct 13, 2010 at 5:01 PM, Mauricio Romero <
mauricio.romero@quantil.com.co> wrote:

> Hi,
>
>
>
> I want to select a subsample from my data, choosing one record from each
> group. I know how to do this with a for.
>
>
>
> For example: lets say I have the data:
>
> A=cbind(rnorm(100),runif(100),(rep(c(1,2,3,4,5),20)))
>
> Where the third column is the group variable. Then what I want is to select
> 5 observations. Each one taken randomly from each group.
>
>
>
>
>
> INDEX =NULL
>
> i=1
>
> for(index_g in  unique(A[,3])){
>
> INDEX [i]=sample(which(A[,3]==index_g),1)
>
> i=i+1
>
> }
>
> SEL=A[INDEX,]
>
>
>
>
>
> Is there a way to do this without a “for”? in other words is there a way to
> “vectorize” this?
>
>
>
> Thank you,
>
>
>
>
>
> Mauricio Romero
>
> Quantil S.A.S.
>
> Bogotá,Colombia
>
> www.quantil.com.co
>
>
>
> "It is from the earth that we must find our substance; it is on the earth
> that we must find solutions to the problems that promise to destroy all
> life
> here"
>
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help@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.
>
>


-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

	[[alternative HTML version deleted]]

