[R] how to apply sample function to each row of a data frame?
wangwallace
talenttree at gmail.com
Sat Nov 20 04:22:57 CET 2010
> 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!!
sorry for the confusion. let me explain it in more details:
actually, what I meant is to draw two random numbers from each row
separately to create a new data frame. for example, an example output could
be:
1 3
4 5
9 8
The key is these two numbers in each row should be randomized. They don't
have to be drawn from the same column. Specifically, as shown above, two
numbers in the first row belong to columns M and Q, whereas the two numbers
in the second row belong to columns M and P. I am wondering how the two
numbers can be sampled from each row separately.
Finally, since the column names of the sampled two numbers across these
three rows will probably be different, I guess I cannot use rbind to put all
these three rows together. Is there anything else (I don't want use list) I
can use to align three rows with different column names together? Also, if I
can write a function for it. May I use some syntax like the following to
repeat the whole process 1000 times (i.e., 1000 samples)?
> result<-vector("list",1000)
> for(i in 1:1000)result[[i]]<-fff(data)#fff(data) is the function name
> result
Again, I really appreciate your help. without your help, I couldn't have got
this far. :)
--
View this message in context: http://r.789695.n4.nabble.com/Re-how-to-apply-sample-function-to-each-row-of-a-data-frame-tp3050933p3051253.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list