[R] List of "occurrence" matrices

Gabor Grothendieck ggrothendieck at gmail.com
Thu Aug 7 16:06:00 CEST 2008


Tr this:


inner <- function(a,b,f) apply(b,2,function(x)apply(a,2,function(y)f(x,y)))

lapply(levels(DF[, 1]), 	function(x) inner(DF, DF, function(a, b)
sum(a == x & b == x)))


On Thu, Aug 7, 2008 at 9:04 AM, Lauri Nikkinen <lauri.nikkinen at iki.fi> wrote:
> R users,
>
> I don't know if I can make myself clear but I'll give it a try. I have
> a data.frame like this
>
> x <- "var1,var2,var3,var4
> a,b,b,a
> b,b,c,b
> c,a,a,a
> a,b,c,c
> b,a,c,a
> c,c,b,b
> a,c,a,b
> b,c,a,c
> c,a,b,c"
> DF <- read.table(textConnection(x),  header=T, sep=",")
> DF
>
> and I would like to sum all the combinations/occurences by a factor
> (letter in this case) between variables and produce a list of
> "occurrence" matrices. For example in this case the "occurrence"
> matrix (first element of list) for factor "a" should look like this
>
>>occulist
> $a
>        var1    var2    var3    var4
> var1    x       0       1       1
> var2    0       x       1       2
> var3    1       1       x       1
> var4    1       2       1       x
>
> $b
> etc.
>
> because there is two rows where var2 and var4 has "a"
>
> DF[DF$var2=="a" & DF$var4=="a",]
>
> Can you give an advice how to achieve this kind of a list of matrices?
>
> -Lauri
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list