Transform attribute vector into a matrix with differences of elements

*爱你&永不变心* 提交于 2020-01-25 23:39:07

问题


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) 

回答1:


tmp <- c(18, 23, 60)

You can use dist with a couple of arguments:

dist(tmp, upper=TRUE, diag=TRUE)
   1  2  3
1  0  5 42
2  5  0 37
3 42 37  0

Note that the dist function returns a "dist" object, so you might want to coerce it to a matrix with as.matrix. Then you can drop the arguments:

as.matrix(dist(tmp))
   1  2  3
1  0  5 42
2  5  0 37
3 42 37  0

Or again use outer. Feed it the subtraction operator, then take the absolute value.

abs(outer(tmp, tmp, "-"))

     [,1] [,2] [,3]
[1,]    0    5   42
[2,]    5    0   37
[3,]   42   37    0

Presumably, dist will be faster than outer, since the algorithm can take advantage of the symmetry that is present in such a calculation, while outer is more general.



来源:https://stackoverflow.com/questions/46956248/transform-attribute-vector-into-a-matrix-with-differences-of-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!