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.
Hi:> sqrt(0.2)[1] 0.4472136 Evidently, distancevector() is finding the *average* distance between the two vectors. HTH, Dennis On Mon, Aug 8, 2011 at 12:02 AM, vioravis <vioravis at gmail.com> wrote:> 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. >
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
Thank you both for your reply. I went with the cosine function for similarity and used it with apply to get a measure of distance. Ravi -- View this message in context: http://r.789695.n4.nabble.com/Distance-between-a-vector-and-matrix-rows-tp3726268p3726610.html Sent from the R help mailing list archive at Nabble.com.