Hi wangwallace,

Here are two ways of doing it. Could you please be more specific?

# data set
X <- matrix(1:9, ncol = 3, byrow = TRUE)
colnames(X) <- c('M', 'P', 'Q')
X

# option 1  -- two samples per row
replicate(2, {
 out <- t(apply(X, 1, sample))
 colnames(out) <- colnames(X)
 list(out)
 })

# option 2  -- two samples of the whole data
replicate(2,{
X <- X[sample(3),]
 list(X)
 })



HTH,
Jorge


On Fri, Nov 19, 2010 at 1:34 PM, wangwallace <> wrote:

>
> this is a simple question, but I wasn't able to figure it out myself.
>
> here is the data frame:
>
> M P Q
> 1 2 3
> 4 5 6
> 7 8 9
>
> M, P, Q each represent a variable
>
> I want to draw 2 random sample from each row separately to create a new
> data
> frame. how can I do it?
>
> thanks!!
> --
> View this message in context:
> http://r.789695.n4.nabble.com/how-to-apply-sample-function-to-each-row-of-a-data-frame-tp3050787p3050787.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>

        [[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.

Reply via email to