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

VinceD vincent.degove at laposte.net
Thu Jul 10 15:11:42 CEST 2008


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.



More information about the R-help mailing list