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))
}
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 <-
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
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
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
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, "-"),
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
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
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
9 matches
Mail list logo