Skip to content
Prev 370020 / 398503 Next

R version 3.3.2, Windows 10: Applying a function to each possible pair of rows from two different data-frames

You appear to be trying to write C code in R. Don't do this. If you
can trade off space for efficiency, the calculation can be easily
vectorized (assuming I correctly understand what you want to do, of
course).

set.seed(135) ## for reproducibility
D1<-data.frame(x=1:5,y=6:10,z=rnorm(5))
D2<-data.frame(x=19:30,y=41:52,z=rnorm(12))

D.all <-merge(D1,D2, by.x=NULL,by.y=NULL) ## Cartesian product of the two frames

D.all$distance <- sqrt(rowSums((D.all[,1:2] - D.all[,4:5])^2)) ## note
use of rowSums
D.all$difference <- (D.all[,3] - D.all[,6])^2

D.all



Cheers,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Fri, Jun 23, 2017 at 8:19 AM, Rathore, Saubhagya Singh
<saubhagya at gatech.edu> wrote:
Message-ID: <CAGxFJbQ6V4SmVjruMh=gmS5AXi2Bv5Atuj7Za2cOcMjpk0GXMQ@mail.gmail.com>
In-Reply-To: <DM5PR07MB3179182A78ED597F88709C7BCFD80@DM5PR07MB3179.namprd07.prod.outlook.com>