Re: [R] build matrix with the content of one column of a data frame in function of two factors

From: VinceD <vincent.degove_at_laposte.net>
Date: Thu, 10 Jul 2008 06:11:42 -0700 (PDT)

So the solution is : tapply(content, list(factor1, factor2), mean)

An example of what it does :
> my.data

             name  item vote
        1 Ricardo  Coke   20
        2 Ricardo Fanta   60
        3 Ricardo Pepsi  100
        4   Marie Pepsi   40
        5   Marie  Coke   60
        6   Julia Fanta   60
        7   Julia  Coke  100


> attach(my.data)
> tapply(vote, list(name, item), mean) -> tastes > tastes Coke Fanta Pepsi Julia 100 60 NA Marie 60 NA 40 Ricardo 20 60 100

And then, you can compute the distance between people if you want :
> dist(tastes, diag = T)

                   Julia    Marie  Ricardo
        Julia    0.00000                  
        Marie   69.28203  0.00000         
        Ricardo 97.97959 88.31761  0.00000

That's it !

-- 
View this message in context: http://www.nabble.com/build-matrix-with-the-content-of-one-column-of-a-data-frame-in-function-of-two-factors-tp18364752p18382632.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
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.
Received on Thu 10 Jul 2008 - 13:37:40 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 10 Jul 2008 - 14:31:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive