Re: [R] avoid a loop

2010-11-04 Thread Joshua Wiley
And to wrap it up and help you choose, here are four functions based on these emails (the first one is my own slight variant): library(ecodist) a <- sample(1:1000, 10^4, replace = TRUE) b <- sample(letters[1:6], 10^4, replace = TRUE) foo1 <- function() { x <- table(a, b) return(x %*% t(x)) }

Re: [R] avoid a loop

2010-11-04 Thread Dennis Murphy
Hi: To mimic Sarah Goslee's reply within base R, either of these work: crossprod(t(as.matrix(xtabs( ~ a + b crossprod(t(as.matrix(table(a, b HTH, Dennis On Thu, Nov 4, 2010 at 12:42 PM, cory n wrote: > Let's suppose I have userids and associated attributes... columns a and b > > a <-

Re: [R] avoid a loop

2010-11-04 Thread David Winsemius
On Nov 4, 2010, at 4:24 PM, Sarah Goslee wrote: Here's one possibility: library(ecodist) a <- c(1,1,1,2,2,3,3,3,3) b <- c("a","b","c","a","d","a", "b", "e", "f") x <- crosstab(a, b, rep(1, length(a))) x a b c d e f 1 1 1 1 0 0 0 2 1 0 0 1 0 0 3 1 1 0 0 1 1 x %*% t(x) 1 2 3 1 3 1 2 2 1 2

Re: [R] avoid a loop

2010-11-04 Thread Sarah Goslee
Here's one possibility: > library(ecodist) > a <- c(1,1,1,2,2,3,3,3,3) > b <- c("a","b","c","a","d","a", "b", "e", "f") > > x <- crosstab(a, b, rep(1, length(a))) > x a b c d e f 1 1 1 1 0 0 0 2 1 0 0 1 0 0 3 1 1 0 0 1 1 > x %*% t(x) 1 2 3 1 3 1 2 2 1 2 1 3 2 1 4 Sarah On Thu, Nov 4, 2010 at

[R] avoid a loop

2010-11-04 Thread cory n
Let's suppose I have userids and associated attributes... columns a and b a <- c(1,1,1,2,2,3,3,3,3) b <- c("a","b","c","a","d","a", "b", "e", "f") so a unique list of a would be id <- unique(a) I want a matrix like this... [,1] [,2] [,3] [1,]312 [2,]121 [3,]2

Re: [R] avoid a loop

2009-05-28 Thread Martin Morgan
Gabor Grothendieck wrote: Try this: sapply(1:n, function(i) sum(abs(outer(a, b, "-")-i)==0)) [1] 10 10 10 10 9 abs(outer(a, b, "-") - i) == 0 ==> outer(a, b, "-") == i. sum(outer(a, b, "-") == i) asks how many times i is an element of outer(a, b, "-"). This is tabulate(outer(a, b, "-"),

Re: [R] avoid a loop

2009-05-28 Thread Gabor Grothendieck
Try this: > sapply(1:n, function(i) sum(abs(outer(a, b, "-")-i)==0)) [1] 10 10 10 10 9 On Thu, May 28, 2009 at 5:45 PM, KARAVASILIS GEORGE wrote: > Hello, R users. > I have the following code: > > a=1:10 > b=-3:15 > n=5 > x <- rep(0,n) > for (i in 1:n) x[i] <- sum( outer(a,b, function(s,t)  ab

Re: [R] avoid a loop

2009-05-28 Thread David Winsemius
On May 28, 2009, at 5:45 PM, KARAVASILIS GEORGE wrote: Hello, R users. I have the following code: a=1:10 b=-3:15 n=5 x <- rep(0,n) for (i in 1:n) x[i] <- sum( outer(a,b, function(s,t) abs(a-b-i)==0) ) You don't seem to be doing anything with s and t? Did you mean: for (i in 1:n) x[i] <- su

[R] avoid a loop

2009-05-28 Thread KARAVASILIS GEORGE
Hello, R users. I have the following code: a=1:10 b=-3:15 n=5 x <- rep(0,n) for (i in 1:n) x[i] <- sum( outer(a,b, function(s,t) abs(a-b-i)==0) ) Can someone tell me if I could avoid the for command? Thank you in advance. __ R-help@r-project.org mai