Home > other >  How to calculate all pairwise difference for multiple varibles
How to calculate all pairwise difference for multiple varibles

Time:10-30

I need to calculate all pairwise difference for each of these variables (there are 100 in my dataset):

image

Then I want to summarize (all) the values of each of the pairwise sums and arrange them in a in a matrix.

CodePudding user response:

We may use outer if we need a matrix

outer(seq_along(df1), seq_along(df1), FUN =
      Vectorize(function(i, j) sum(df1[[i]] - df1[[j]], na.rm = TRUE)))

-output

 [,1]  [,2]  [,3]
[1,]   0.00 47.80 56.49
[2,] -47.80  0.00  8.69
[3,] -56.49 -8.69  0.00

Or if we don't need redundant comparison, use combn

combn(df1, 2, FUN = function(x) sum(x[[1]] - x[[2]], na.rm = TRUE))

-output

[1] 47.80 56.49  8.69

data

df1 <- structure(list(V1 = c(67.81, 65.33, 54.67, 53.2, 53.77, 52.66, 
50.77, 47.84, 46.33, 44.15), V2 = c(57.68, 56.58, 52.61, 49.74, 
49.28, 48.03, 46.15, 43.96, 42.76, 41.94), V3 = c(54.04, 54.34, 
52.36, 49.34, 48.93, 48.06, 46.21, 43.51, 42.15, 41.1)),
 class = "data.frame", row.names = c(NA, 
-10L))

CodePudding user response:

An alternative is using dist. With the help of akrun we get:

dist(t(df1), method = "manhattan")
      V1    V2
V2 47.80      
V3 56.49  8.87
  • Related