[R] distance function (analogue)

Gavin Simpson gavin.simpson at ucl.ac.uk
Tue Jul 17 18:10:50 CEST 2007


On Tue, 2007-07-17 at 14:32 +0200, Birgit Lemcke wrote:
> Hello R-Users,
> 
> its again me with a question.
> Im using R 2.5.0 on Mac Power Book running Mac OS X 10.4.10

And again, you should send this to the maintainer (that'd be me), *not*
R-help! Unless you are asking help on a widely used package, it is
unlikely that anyone on R-Help can help you. And I'm not being modest
when I say that analogue is unlikely to fit that description.

> 
> I try to calculate distances betweeen two data tables looking like this
> 
>      V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13 V14 V15 V16 V17 V18  
> V19 V20 V21 V22 V23 V24 V25 V26 V27 V28 V29 V30 V31 V32 V33 V34 V35 V36
> 1    1  0  0  0  1  1  0  1  0   0   0   1   0   0   0   0   0   1    
> 0   0   1   0   0   0   0   1   1   0   1   1   0   0   0   0   0   0
> 2    1  1  0  0  1  1  0  0  1   0   1   1   0   0   0   0   0   1    
> 0   0   1   0   0   0   0   1   1   0   0   1   0   0   1   1   0   0
> 3    1  1  0  0  1  1  1  1  0   0   1   0   0   0   0   0   0   1    
> 0   0   1   1   0   0   0   0   1   0   1   0   0   1   0   0   0   0
> 4    0  1  0  0  1  1  0  1  0   0   0   1   0   0   0   0   0   1    
> 0   0   1   0   0   1   0   0   1   0   0   1   0   0   1   0   0   0
> 5    0  1  0  0  1  1  0  0  1   0   0   1   1   0   0   0   0   1    
> 0   0   1   0   0   0   0   1   0   0   0   1   0   0   0   0   0   0
> 6    1  1  0  0  1  1  0  1  1   0   1   0   0   0   0   0   1   1    
> 0   0   1   0   0   0   0   0   1   0   0   1   0   1   1   0   0   0

I need to know what R thinks the objects look like, not how you think
they look.

> As i know I did the same 2 weeks ago and it worked properly.
> 
> Here are my codes:
> 
> Table1<-read.table("Alle_DatenFemBeschr4a.csv", sep = ";")
> 
> Table0<-read.table("Alle_DatenMalBeschr4a.csv", sep = ";")
> 
> Dist.Gower<- distance(Table1 ,Table0 ,method ="mixed", weights = c 
> (0.3333333, 0.3333333, 0.3333333, 0.5000000, 0.5000000, 0.5000000,  
> 0.5000000, 0.5000000, 0.5000000, 0.2500000, 0.2500000, 0.2500000,  
> 0.2500000, 0.1428571, 0.1428571, 0.1428571, 0.1428571, 0.1428571,  
> 0.1428571, 0.1428571, 0.3333333, 0.3333333, 0.3333333, 0.2000000,  
> 0.2000000, 0.2000000, 0.2000000, 0.2000000, 0.1111111, 0.1111111,  
> 0.1111111, 0.1111111, 0.1111111, 0.1111111, 0.1111111, 0.1111111,  
> 0.1111111, 0.2000000, 0.2000000, 0.2000000, 0.2000000, 0.2000000,  
> 0.1428571, 0.1428571, 0.1428571, 0.1428571, 0.1428571, 0.1428571,  
> 0.1428571, 0.1428571, 0.1428571, 0.1428571, 0.1428571, 0.1428571,  
> 0.1428571, 0.1428571, 0.2000000, 0.2000000, 0.2000000, 0.2000000,  
> 0.2000000, 0.5000000, 0.5000000, 0.5000000, 0.5000000, 0.5000000,  
> 0.5000000, 0.5000000, 0.5000000, 0.5000000, 0.5000000, 0.5000000,  
> 0.5000000, 0.5000000, 0.5000000, 0.5000000, 0.5000000, 0.5000000,  
> 0.5000000, 0.5000000, 0.5000000, 0.5000000, 0.5000000, 0.5000000,  
> 0.5000000))
> 
> This produces the following massage:
> 
> Fehler in distance(Table1, Table0, method = "mixed", weights = c 
> (0.3333333,  :
> 	Different columns (species) are coded as factors in 'x' and 'y'

This means that Table1 has columns coded as factors that are not coded
factors in Table0 or vice versa.

Do 

> str(Table1)
> str(Table0)

and compare the output.

Alternatively, send me the two csv files you are reading in and I'll try
to track down the problem for you.

G

Ps: It would be easier to read if you did

my.weights <- c (0.3333333, 0.3333333, 0.3333333, 0.5000000, 0.5000000,
0.5000000, 0.5000000, 0.5000000, 0.5000000, 0.2500000, 0.2500000,
0.2500000, 0.2500000, 0.1428571, 0.1428571, 0.1428571, 0.1428571,
0.1428571, 0.1428571, 0.1428571, 0.3333333, 0.3333333, 0.3333333,
0.2000000, 0.2000000, 0.2000000, 0.2000000, 0.2000000, 0.1111111,
0.1111111, 0.1111111, 0.1111111, 0.1111111, 0.1111111, 0.1111111,
0.1111111, 0.1111111, 0.2000000, 0.2000000, 0.2000000, 0.2000000,
0.2000000, 0.1428571, 0.1428571, 0.1428571, 0.1428571, 0.1428571,
0.1428571, 0.1428571, 0.1428571, 0.1428571, 0.1428571, 0.1428571,
0.1428571, 0.1428571, 0.1428571, 0.2000000, 0.2000000, 0.2000000,
0.2000000, 0.2000000, 0.5000000, 0.5000000, 0.5000000, 0.5000000,
0.5000000, 0.5000000, 0.5000000, 0.5000000, 0.5000000, 0.5000000,
0.5000000, 0.5000000, 0.5000000, 0.5000000, 0.5000000, 0.5000000,
0.5000000, 0.5000000, 0.5000000, 0.5000000, 0.5000000, 0.5000000,
0.5000000, 0.5000000)

Dist.Gower <- distance(Table1, Table0, method ="mixed", weights =
my.weights)

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
 Gavin Simpson                 [t] +44 (0)20 7679 0522
 ECRC, UCL Geography,          [f] +44 (0)20 7679 0565
 Pearson Building,             [e] gavin.simpsonATNOSPAMucl.ac.uk
 Gower Street, London          [w] http://www.ucl.ac.uk/~ucfagls/
 UK. WC1E 6BT.                 [w] http://www.freshwaters.org.uk
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list