[R] Distance between a vector and matrix rows

Eik Vettorazzi E.Vettorazzi at uke.uni-hamburg.de
Mon Aug 8 12:00:53 CEST 2011


Hi Ravi,
I don't know about other packages to calculate euclidian distances but
this can be done rather easily by hand:

ref<-rep(1,5)
apply(mydata,1,function(x)sqrt(sum((x-ref)^2)))

cheers


Am 08.08.2011 09:02, schrieb vioravis:
> I am trying to find the distance between a vector and each row of a
> dataframe. I am using the function "distancevector" in the package "hopach"
> as follows:
> 
> mydata<-as.data.frame(matrix(c(1,1,1,1,0,1,1,1,1,0),nrow=2))
> 
>  V1 V2 V3 V4 V5
> 1  1  1  0  1  1
> 2  1  1  1  1  0
> vec <- c(1,1,1,1,1)
> d2<-distancevector(mydata,vec,d="euclid")
> 
> The Euclidean distance between the two rows of the data frame to the vector
> should be 1. But I am getting 0.4472136 for both.
> 
> Can someone please point out the reason for the discrepancy???
> 
> Also, are there other packages for calculating the distance between a binary
> vector and all the rows of a data frame (contains only binary values)? 
> 
> Thank you.
> 
> Ravi
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Distance-between-a-vector-and-matrix-rows-tp3726268p3726268.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.


-- 
Eik Vettorazzi
Institut für Medizinische Biometrie und Epidemiologie
Universitätsklinikum Hamburg-Eppendorf

Martinistr. 52
20246 Hamburg

T ++49/40/7410-58243
F ++49/40/7410-57790



More information about the R-help mailing list