wangwallace
2010-Nov-19 18:34 UTC
[R] how to apply sample function to each row of a data frame?
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.
Jorge Ivan Velez
2010-Nov-19 20:23 UTC
[R] how to apply sample function to each row of a data frame?
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]]
Petr Savicky
2010-Nov-19 21:01 UTC
[R] how to apply sample function to each row of a data frame?
On Fri, Nov 19, 2010 at 10:34:26AM -0800, 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?I am not sure, what you exactly mean. Can you provide an example of the expected output? If you consider each row as a sample of size 3 and want to use the function sample() or sample(, replace=TRUE) to create a new sample of the same size as a row of a new table, then in addition to the already posted solutions you can use X <- matrix(1:9, ncol = 3, byrow = TRUE) j <- rep(seq(nrow(X)), each=2) Y <- matrix(, ncol=ncol(X), nrow=length(j)) for (i in seq(nrow(Y))) { Y[i, ] <- sample(X[j[i], ]) # or add replace=TRUE } colnames(Y) <- c("M", "P", "Q") data.frame(Y) M P Q 1 2 3 1 2 1 3 2 3 5 4 6 4 6 4 5 5 7 9 8 6 8 9 7 PS.
wangwallace
2010-Nov-20 03:22 UTC
[R] how to apply sample function to each row of a data frame?
> 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 > resultAgain, 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.
Petr Savicky
2010-Nov-22 09:54 UTC
[R] how to apply sample function to each row of a data frame?
On Sun, Nov 21, 2010 at 12:43:21PM -0800, wangwallace wrote:> here is the data frame: > > a b c A B C > [1,] 1 2 3 4 5 6 > [2,] 7 8 9 10 11 12 > [3,] 13 14 15 16 17 18 > > a, b, c are type I variables > A, B, C are type II variables > each row represent the data from one subject > > my purpose is to create a new data frame in which: > > 1) in each row, there are one random number from type I variables, and two > random numbers from type II variables > > 2) meanwhile, in each row, the two type II numbers have to be only those > numbers that are not corresponding to the type I number. For example, if the > type I number is 1, the type II numbers should not include 4. > > 3) type I number and type II numbers in each row should be all from the same > subject. > > the new data frame should be like this: > > [,1] [,2] [,3] > [1,] I II II > [2,] I II II > [3,] I II IIIf the two type II objects in a row should be always different, then this may be computed for example as follows. # prepare the input A <- matrix(1:18, ncol=6, byrow=TRUE) colnames(A) <- c(letters[1:3], LETTERS[1:3]) # prepare random indices for each row ind <- t(replicate(nrow(A), sample(3))) # construct the output without a cycle col1 <- A[cbind(seq(nrow(A)), ind[, 1])] col2 <- A[cbind(seq(nrow(A)), 3 + ind[, 2])] col3 <- A[cbind(seq(nrow(A)), 3 + ind[, 3])] B <- cbind(col1, col2, col3) # or with a cycle over rows C <- matrix(nrow=nrow(A), ncol=3) for (i in seq(nrow(A))) { C[i, 1] <- A[i, ind[i, 1]] C[i, 2:3] <- A[i, 3 + ind[i, 2:3]] } Petr Savicky.
wangwallace
2010-Nov-22 17:56 UTC
[R] how to apply sample function to each row of a data frame?
I tried it. it works out perfectly. you save my life. -- View this message in context: http://r.789695.n4.nabble.com/Re-how-to-apply-sample-function-to-each-row-of-a-data-frame-tp3050933p3054117.html Sent from the R help mailing list archive at Nabble.com.