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?
source
share