如何计算 R 中许多变量之间的所有成对 abs 差异
How to calculate all pairwise abs differences among many variables in R
我需要计算许多变量之间的所有成对绝对差异(我的数据集中有 100 个)。
我希望每个差异得到一列:
我试过下一个函数但是它总结了结果,我不需要总和,但是所有的单个abs差异。
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))))
数据
df <- data.frame(v1=1:6,V2=7:12,V3=13:18,v4=19:24,v5=25:30,v6=31:36)
也许combn
会给你想要的。就其本身而言,它只报告组合,但您可以提供一个传递单个参数的函数,两个参数的向量长度为 2 表示。
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
比如第一次调用anon-func时,i
就是c(1L, 2L)
。
可能让您恼火的是,当您删除 sum
时,outer
不起作用(我相信您试过了)。那是因为Vectorize
的结果不能化简为矩阵(默认),所以我们可以设置为FALSE
r <- outer(seq_along(df), seq_along(df),
FUN=Vectorize(function(i, j) abs(df[[i]] - df[[j]]), SIMPLIFY=FALSE))
结果
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
我需要计算许多变量之间的所有成对绝对差异(我的数据集中有 100 个)。 我希望每个差异得到一列:
我试过下一个函数但是它总结了结果,我不需要总和,但是所有的单个abs差异。
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))))
数据
df <- data.frame(v1=1:6,V2=7:12,V3=13:18,v4=19:24,v5=25:30,v6=31:36)
也许combn
会给你想要的。就其本身而言,它只报告组合,但您可以提供一个传递单个参数的函数,两个参数的向量长度为 2 表示。
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
比如第一次调用anon-func时,i
就是c(1L, 2L)
。
可能让您恼火的是,当您删除 sum
时,outer
不起作用(我相信您试过了)。那是因为Vectorize
的结果不能化简为矩阵(默认),所以我们可以设置为FALSE
r <- outer(seq_along(df), seq_along(df),
FUN=Vectorize(function(i, j) abs(df[[i]] - df[[j]]), SIMPLIFY=FALSE))
结果
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