How to calculate all pairwise abs differences among many variables in R

Viewed 50

I need to calculate all pairwise abs differences among many variables (there are 100 in my dataset). I expect to get one column for each difference:

I have tried the next function but it summarizes the results, and I do not need the sum, but all the single abs differences.

outer(seq_along(rio_csv), seq_along(rio_csv), FUN =
              Vectorize(function(i, j)abs(sum(rio_csv[[i]] - rio_csv[[j]], na.rm = FALSE))))

data

df <- data.frame(v1=1:6,V2=7:12,V3=13:18,v4=19:24,v5=25:30,v6=31:36)
2 Answers

What probably irritated you is that outer did not work when you delete the sum (I'm sure you tried that). That's because the Vectorize result can not be simplified into a matrix (the default), so we may set it to FALSE

r <- outer(seq_along(df), seq_along(df),
           FUN=Vectorize(function(i, j) abs(df[[i]] - df[[j]]), SIMPLIFY=FALSE))

Result

matrix(unlist(r), nrow(df))
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26] [,27] [,28] [,29] [,30] [,31] [,32] [,33] [,34] [,35] [,36]
# [1,]    0    6   12   18   24   30    6    0    6    12    18    24    12     6     0     6    12    18    18    12     6     0     6    12    24    18    12     6     0     6    30    24    18    12     6     0
# [2,]    0    6   12   18   24   30    6    0    6    12    18    24    12     6     0     6    12    18    18    12     6     0     6    12    24    18    12     6     0     6    30    24    18    12     6     0
# [3,]    0    6   12   18   24   30    6    0    6    12    18    24    12     6     0     6    12    18    18    12     6     0     6    12    24    18    12     6     0     6    30    24    18    12     6     0
# [4,]    0    6   12   18   24   30    6    0    6    12    18    24    12     6     0     6    12    18    18    12     6     0     6    12    24    18    12     6     0     6    30    24    18    12     6     0
# [5,]    0    6   12   18   24   30    6    0    6    12    18    24    12     6     0     6    12    18    18    12     6     0     6    12    24    18    12     6     0     6    30    24    18    12     6     0
# [6,]    0    6   12   18   24   30    6    0    6    12    18    24    12     6     0     6    12    18    18    12     6     0     6    12    24    18    12     6     0     6    30    24    18    12     6     0

Perhaps combn will give you want you want. By itself, it just reports the combinations, but you can provide a function that is passed a single argument, a vector length-2 of the two indicates.

combn(6,2)
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
# [1,]    1    1    1    1    1    2    2    2    2     3     3     3     4     4     5
# [2,]    2    3    4    5    6    3    4    5    6     4     5     6     5     6     6
combn(6, 2, FUN = function(i) df[,i[1]] - df[,i[2]])
#      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
# [1,]   -6  -12  -18  -24  -30   -6  -12  -18  -24    -6   -12   -18    -6   -12    -6
# [2,]   -6  -12  -18  -24  -30   -6  -12  -18  -24    -6   -12   -18    -6   -12    -6
# [3,]   -6  -12  -18  -24  -30   -6  -12  -18  -24    -6   -12   -18    -6   -12    -6
# [4,]   -6  -12  -18  -24  -30   -6  -12  -18  -24    -6   -12   -18    -6   -12    -6
# [5,]   -6  -12  -18  -24  -30   -6  -12  -18  -24    -6   -12   -18    -6   -12    -6
# [6,]   -6  -12  -18  -24  -30   -6  -12  -18  -24    -6   -12   -18    -6   -12    -6

For instance, the first time the anon-func is called, i is c(1L, 2L).

Related