[R] how to convert a data.frame to a list of dist objects for individual differences MDS?

Michael Friendly friendly at yorku.ca
Tue Mar 22 18:19:21 CET 2011


On 3/22/2011 12:47 PM, Phil Spector wrote:
> Michael -
>     I think this does what you want:

*Perfectly*

>
> helm.raw <-
> read.table("http://euclid.psych.yorku.ca/datavis/Private/mdshelm.dat",header=TRUE,
> row.names=1)
> trans =
> c('A'='RPur','C'='Red','E'='Yel','G'='Gy1','I'='Gy2','K'='Green','M'='Blue','O'='BlP','Q'='Pur1','S'='Pur2')
>
> cnames = do.call(rbind,strsplit(rownames(helm.raw), ""))
> cnames = apply(cnames,2,function(x)trans[x])
> uu = unique(as.vector(cnames))
>
> onecol = function(col){
>     themat = matrix(NA,10,10)
>     dimnames(themat) = list(uu,uu)
>     themat[cnames] = col
>     as.dist(t(themat))
> }
>
> result = lapply(as.data.frame(helm.raw),onecol)

Thank you so much for this.  Using the onecol function was the main 
trick I couldn't see.

-Michael


-- 
Michael Friendly     Email: friendly AT yorku DOT ca
Professor, Psychology Dept.
York University      Voice: 416 736-5115 x66249 Fax: 416 736-5814
4700 Keele Street    Web:   http://www.datavis.ca
Toronto, ONT  M3J 1P3 CANADA



More information about the R-help mailing list