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

From: Michael Friendly <friendly_at_yorku.ca>
Date: Tue, 22 Mar 2011 13:19:21 -0400

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

______________________________________________
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 Tue 22 Mar 2011 - 17:22:31 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 Tue 22 Mar 2011 - 17:30:24 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