Similarly to this previous post I need to transfrom an attribute vector into a matrix. This time with differences between pairs of elements using R.
For example I have a vector which reports the age of N people (from 18 to 90 years). I need to convert this vector into a NxN matrix named A (with people names on rows and columns), where each cell Aij has the value of |age_i-age_j|, representing the absolute difference in age between the two people i and j.
Here is an example with 3 persons, first 18 yo, second 23 yo, third 60 yo, which produce this vector:
c(18, 23, 60)
I want to transform it into this matrix:
A = matrix( c(0, 5, 42, 5, 0, 37, 42, 37, 0), nrow=3, ncol=3, byrow = TRUE)