AP38
AP38

Reputation: 169

Pairwise substration table

My dataset looks like this:

Col1 Col2
A     2
B     5
C     1
D     4

I would like to substract rows from Col2 so that the final output looks like this:

    A   B   C   D
A   0  -3   1  -2
B   3   0   4   1
C  -1  -4   0  -3
D   2  -1   3   0

I would appreciate some help as I am struggling formatting the results into a pairwise comparison table.

Upvotes: 0

Views: 51

Answers (1)

AP38
AP38

Reputation: 169

Using outer is definitely a good option. For instance:

x2 <- 1:4; names(x2) <- c("A","B","C","D")
x3 <- 1:4; names(x3) <- c("A","B","C","D")

outer(x2,x3, "-")

  [,1] [,2] [,3] [,4]
A    0   -1   -2   -3
B    1    0   -1   -2
C    2    1    0   -1
D    3    2    1    0

Thanks @jogo for the suggestion

Upvotes: 2

Related Questions