Re: [R] Need a vectorized way to avoid two nested FOR loops

2009-10-08 Thread Dimitris Rizopoulos
age- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Dimitris Rizopoulos Sent: Thursday, October 08, 2009 6:28 AM To: joris meys Cc: r-help@r-project.org; Rama Ramakrishnan Subject: Re: [R] Need a vectorized way to avoid two nested FOR loops Another approach is:

Re: [R] Need a vectorized way to avoid two nested FOR loops

2009-10-08 Thread Rama Ramakrishnan
rg [mailto:r-help-boun...@r- project.org] On Behalf Of Dimitris Rizopoulos Sent: Thursday, October 08, 2009 6:28 AM To: joris meys Cc: r-help@r-project.org; Rama Ramakrishnan Subject: Re: [R] Need a vectorized way to avoid two nested FOR loops Another approach is: n <- 20 set.seed(2) x &l

Re: [R] Need a vectorized way to avoid two nested FOR loops

2009-10-08 Thread Bert Gunter
.@r-project.org] On Behalf Of Dimitris Rizopoulos Sent: Thursday, October 08, 2009 6:28 AM To: joris meys Cc: r-help@r-project.org; Rama Ramakrishnan Subject: Re: [R] Need a vectorized way to avoid two nested FOR loops Another approach is: n <- 20 set.seed(2) x <- as.data.frame(matrix(sample(1

Re: [R] Need a vectorized way to avoid two nested FOR loops

2009-10-08 Thread Dimitris Rizopoulos
Another approach is: n <- 20 set.seed(2) x <- as.data.frame(matrix(sample(1:2, n*6, TRUE), nrow = n)) x.col <- c(1, 3, 5) values <- do.call(paste, c(x[x.col], sep = "\r")) out <- lapply(seq_along(ind), function (i) { ind <- which(values == values[i]) ind[!ind %in% i] }) out Best, Dimit

Re: [R] Need a vectorized way to avoid two nested FOR loops

2009-10-08 Thread joris meys
Neat piece of code, Jim, but it still uses a nested loop. If you order the matrix first, you only need one passage through the whole matrix to find the information you need. Off course I don't take into account the ordering. If the ordering algorithm doesn't work in linear time, then it doesn't re

Re: [R] Need a vectorized way to avoid two nested FOR loops

2009-10-08 Thread jim holtman
I answered the wrong question. Here is the code to find all the matches for each row: n <- 20 set.seed(2) # create test dataframe x <- as.data.frame(matrix(sample(1:2,n*6, TRUE), nrow=n)) x x.col <- c(1,3,5) # match against all the other rows x.match1 <- apply(x[, x.col], 1, function(a){ .ma

Re: [R] Need a vectorized way to avoid two nested FOR loops

2009-10-08 Thread jim holtman
Here is one way of doing it: > n <- 20 > set.seed(2) > # create test dataframe > x <- as.data.frame(matrix(sample(1:2,n*6, TRUE), nrow=n)) > x V1 V2 V3 V4 V5 V6 1 1 2 2 2 1 1 2 2 1 1 2 2 1 3 2 2 1 2 1 2 4 1 1 1 1 1 2 5 2 1 2 2 1 1 6 2 1 2 1 2 2 7 1 1