[R] Distance calculation

arun smartpink111 at yahoo.com
Fri Mar 22 14:46:18 CET 2013


Hi Elisa,

I hope this is what you wanted.


dat1<-read.csv("peaks.csv",sep=",")
#Subset
dat2<-dat1[1:5,]
res1<-do.call(cbind,lapply(seq_len(nrow(dat2)),function(i) do.call(rbind,lapply(split(rbind(dat2[i,],dat2[-i,]),1:nrow(rbind(dat2[i,],dat2[-i,]))), function(x) {x1<-rbind(dat2[i,],x); abs((x1$Peak1.v.[1]-x1$Peak1.v.[2])*(x1$Peak1.t.[1]-x1$Peak1.t.[2]))+abs((x1$Peak2.v.[1]-x1$Peak2.v.[2])*(x1$Peak2.t.[1]-x1$Peak2.t.[2]))+abs((x1$Npeak1.v.[1]-x1$Npeak1.v.[2])*(x1$Npeak1.t.[1]-x1$Npeak1.t.[2]))+abs((x1$Npeak2.v.[1]-x1$Npeak2.v.[2])*(x1$Npeak2.t.[1]-x1$Npeak2.t.[2]))}))))
res2<-do.call(cbind,lapply(seq_len(ncol(res1)),function(i) c(c(tail(res1[seq(1,i,1),i],-1),0),res1[-c(1:i),i])))
row.names(res2)<-1:nrow(res2)
 res2
#      [,1]     [,2]     [,3]     [,4]     [,5]
#1   0.0000   0.0000   0.0000 379.1364   0.0000
#2   0.0000   0.0000   0.0000 312.8267   0.0000
#3   0.0000   0.0000   0.0000 379.6576   0.0000
#4 379.1364 312.8267 379.6576   0.0000 324.4063
#5   0.0000   0.0000   0.0000 324.4063   0.0000

resWhole<-do.call(cbind,lapply(seq_len(nrow(dat1)),function(i) do.call(rbind,lapply(split(rbind(dat1[i,],dat1[-i,]),1:nrow(rbind(dat1[i,],dat1[-i,]))), function(x) {x1<-rbind(dat1[i,],x); abs((x1$Peak1.v.[1]-x1$Peak1.v.[2])*(x1$Peak1.t.[1]-x1$Peak1.t.[2]))+abs((x1$Peak2.v.[1]-x1$Peak2.v.[2])*(x1$Peak2.t.[1]-x1$Peak2.t.[2]))+abs((x1$Npeak1.v.[1]-x1$Npeak1.v.[2])*(x1$Npeak1.t.[1]-x1$Npeak1.t.[2]))+abs((x1$Npeak2.v.[1]-x1$Npeak2.v.[2])*(x1$Npeak2.t.[1]-x1$Npeak2.t.[2]))}))))
res2Whole<-do.call(cbind,lapply(seq_len(ncol(resWhole)),function(i) c(c(tail(resWhole[seq(1,i,1),i],-1),0),resWhole[-c(1:i),i])))
row.names(res2Whole)<-1:nrow(res2Whole)
dim(res2Whole)
#[1] 124 124
res2Whole[1:5,1:5]
#      [,1]     [,2]     [,3]     [,4]     [,5]
#1   0.0000   0.0000   0.0000 379.1364   0.0000
#2   0.0000   0.0000   0.0000 312.8267   0.0000
#3   0.0000   0.0000   0.0000 379.6576   0.0000
#4 379.1364 312.8267 379.6576   0.0000 324.4063
#5   0.0000   0.0000   0.0000 324.4063   0.0000

A.K.



________________________________
From: eliza botto <eliza_botto at hotmail.com>
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com> 
Sent: Friday, March 22, 2013 8:26 AM
Subject: 



Dear Arun,
I hope you are fine. 
 the attached text file has my recent question and excel file contains the data. 


thanks in advance

Elisa



More information about the R-help mailing list