Hi
a litle bit different story. But
x1 <- sample(c(rep("red",400),rep("green", 100),
rep("black",300)),100)
is maybe close. With data frame (if it is not big)
> DF
color sample1 sample2 sample3
1 red 400 3002500
2 green 100 0 200
3 black 3001000 500
x
Here's a way using apply(), and the prob= argument of sample():
> df <- data.frame(sample1=c(red=400,green=100,black=300),
sample2=c(300,0,1000), sample3=c(2500,200,500))
> df
sample1 sample2 sample3
red 400 3002500
green 100 0 200
black 3001000 50
I tried all of the approaches below.
the problem with:
> x <- data.frame(matrix(NA,100,3))
> for (i in 2:ncol(DF)) x[,i-1] <- sample(rep(DF[,1], DF[,i]),100)
> if you want result in data frame
> or
> x<-vector("list", 3)
> for (i in 2:ncol(DF)) x[[,i-1]] <- sample(rep(DF[,1], DF[,i]),100)
is tha
Two things to note:
(1) rep() can be vectorized:
> rep(1:3, 2:4)
[1] 1 1 2 2 2 3 3 3 3
>
(2) you will likely get much better performance if you work with
integers and convert to strings after sampling (or use factors), e.g.:
> c("red","green","blue")[sample(rep(1:3,c(400,100,300)), 5)]
[1] "
On Wed, 2006-10-11 at 14:25 -0400, Brian Frappier wrote:
> I tried all of the approaches below.
>
> the problem with:
>
> > x <- data.frame(matrix(NA,100,3))
> > for (i in 2:ncol(DF)) x[,i-1] <- sample(rep(DF[,1], DF[,i]),100)
> > if you want result in data frame
> > or
> > x<-vector("list", 3)
>
Thanks Manuel,
My only problems with the approach you suggest is that it does not seem to
result in a random sample without replacement as it generates a sample based
on the a priori probabilities, not physical selection and deletion from
subsequent sampling. I beleive the sample function would ac
@stat.math.ethz.ch
Subject: Re: [R] Fwd: rarefy a matrix of counts
> Two things to note:
>
> (1) rep() can be vectorized:
> > rep(1:3, 2:4)
> [1] 1 1 2 2 2 3 3 3 3
> >
>
> (2) you will likely get much better performance if you work with
> int
> To: Brian Frappier <[EMAIL PROTECTED]>
> Copies to: Petr Pikal <[EMAIL PROTECTED]>,
> r-help@stat.math.ethz.ch
> Subject:Re: [R] Fwd: rarefy a matrix of counts
>
> > Two things to note:
> >
> > (1) rep(