Manage by line pairs of data frame

I have a data frame in R and I would like to perform a calculation on all pairs of rows. Is there an easier way to do this than using a nested loop?

To make this specific, consider a ten-row data frame, and I want to calculate the difference between all (45) possible pairs.

> data.frame(ID=1:10,Score=4*10:1)
   ID Score
1   1    40
2   2    36
3   3    32
4   4    28
5   5    24
6   6    20
7   7    16
8   8    12
9   9     8
10 10     4

I know I could do this calculation using a nested loop, but is there a way (more R-ish) to do this?

+5
source share
4 answers

Here's another solution using combn:

df <- data.frame(ID=1:10,Score=4*10:1)
cm <- combn(df$ID,2)
delta <- df$Score[cm[1,]]-df$Score[cm[2,]]

or more directly

df <- data.frame(ID=1:10,Score=4*10:1)
delta <- combn(df$ID,2,function(x) df$Score[x[1]]-df$Score[x[2]])
+4
source

To calculate the differences, perhaps you could use

outer(df$Score,df$Score,"-")
+7
source
colmx = matrix(rep(df[,2], 10), ncol=10, byrow=F)
rowmx = matrix(rep(df[,2], 10), ncol=10, byrow=T)
delta = colmx - rowmx
+3

dist() - .

dist(df$Score)

:

as.matrix( dist(df$Score) )
+3

All Articles