编写引用矩阵中元素的函数
Writing a Function That References Elements in a Matrix
我有一个名为“关系”的数据框:
library(tidyverse)
library(igraph)
set.seed(123)
n=10
data = tibble(d = paste(1:n))
relations = tibble(
from = sample(data$d),
to = lead(from, default=from[1]),
)
relations = data.frame(relations)
“关系”数据框如下所示:
from to
1 3 10
2 10 2
3 2 8
4 8 6
5 6 9
6 9 1
7 1 7
8 7 5
9 5 4
10 4 3
然后我有一个名为“m”的矩阵(这个矩阵的维度对应于“关系”数据框):
m<-matrix(rnorm(100) , nrow = 10)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] -1.66794194 -0.6407060 0.07796085 0.7399475 0.6879168 -0.3724388 -0.48378063 0.3104807 0.08473729 -1.2512714
[2,] -0.38022652 -0.8497043 -0.96185663 1.9091036 2.1001089 0.9769734 0.51686204 0.4365235 0.75405379 -0.6111659
[3,] 0.91899661 -1.0241288 -0.07130809 -1.4438932 -1.2870305 -0.3745809 0.36896453 -0.4583653 -0.49929202 -1.1854801
[4,] -0.57534696 0.1176466 1.44455086 0.7017843 0.7877388 1.0527115 -0.21538051 -1.0633261 0.21444531 2.1988103
[5,] 0.60796432 -0.9474746 0.45150405 -0.2621975 0.7690422 -1.0491770 0.06529303 1.2631852 -0.32468591 1.3124130
[6,] -1.61788271 -0.4905574 0.04123292 -1.5721442 0.3322026 -1.2601552 -0.03406725 -0.3496504 0.09458353 -0.2651451
[7,] -0.05556197 -0.2560922 -0.42249683 -1.5146677 -1.0083766 3.2410399 2.12845190 -0.8655129 -0.89536336 0.5431941
[8,] 0.51940720 1.8438620 -2.05324722 -1.6015362 -0.1194526 -0.4168576 -0.74133610 -0.2362796 -1.31080153 -0.4143399
[9,] 0.30115336 -0.6519499 1.13133721 -0.5309065 -0.2803953 0.2982276 -1.09599627 -0.1971759 1.99721338 -0.4762469
[10,] 0.10567619 0.2353866 -1.46064007 -1.4617556 0.5629895 0.6365697 0.03778840 1.1099203 0.60070882 -0.7886028
我想编写一个函数:
选择矩阵“m”中对应于“关系”数据框中每一行的“(从,到)数字”的元素
然后将所有这些元素加在一起
在我的例子中,这看起来像这样:
sums = m[3,10] + m[10,2] + m[2,8] + m[8,6] + m[6,9] + m[9,1] + m[1,7] + m[7,5] + m[5,4] + m[4,3]
sums
[1] -0.8444946
我想也许我可以使用以下“技巧”“引用”矩阵中的正确元素:
sums = m[relations[1,1], relations[1,2]] + m[relations[2,1], relations[2,2]] + m[relations[3,1], relations[3,2]] + m[relations[4,1], relations[4,2]] + m[relations[5,1], relations[5,2]] + m[relations[6,1], relations[6,2]] + m[relations[7,1], relations[7,2]] + m[relations[8,1], relations[8,2]] + m[relations[9,1], relations[9,2]] + m[relations[10,1], relations[10,2]]
Error in m[relations[1, 1], relations[1, 2]] : subscript out of bounds
然后我尝试了这个:
sums = m[as.numeric(relations[1,1]), as.numeric(relations[1,2])] +
m[as.numeric(relations[2,1]), as.numeric(relations[2,2])]
+ m[as.numeric(relations[3,1]), as.numeric(relations[3,2])]
+ m[as.numeric(relations[4,1]), as.numeric(relations[4,2])]
+ m[as.numeric(relations[5,1]), as.numeric(relations[5,2])] +
m[as.numeric(relations[6,1]), as.numeric(relations[6,2])] +
m[as.numeric(relations[7,1]), as.numeric(relations[7,2])] +
m[as.numeric(relations[8,1]), as.numeric(relations[8,2])] +
m[as.numeric(relations[9,1]), as.numeric(relations[9,2])] +
m[as.numeric(relations[10,1]), as.numeric(relations[10,2])]
X9
0.08593303
虽然我想错了,因为
-0.8444946 <> 0.08593303
如何编写一个函数,自动对任何“关系”和“m”执行以下操作?
sums = m[3,10] + m[10,2] + m[2,8] + m[8,6] + m[6,9] + m[9,1] + m[1,7] + m[7,5] + m[5,4] + m[4,3]
谢谢!
您可以使用 [
为矩阵 m
编制索引,而另一个矩阵的列对应于 m
的行和列,就像您的 relations
对象一样。所以我们需要做的就是将 relations
隐藏到 matrix
,用它来索引 m
,然后 sum
全部:
my_sums = function(relations, m) {
r = as.matrix(relations)
if(mode(r) != "numeric") mode(r) = "numeric"
sum(m[r])
}
my_sums(relations, m)
(未经测试,因为您没有共享 m
,但应该可以正常工作。)
我有一个名为“关系”的数据框:
library(tidyverse)
library(igraph)
set.seed(123)
n=10
data = tibble(d = paste(1:n))
relations = tibble(
from = sample(data$d),
to = lead(from, default=from[1]),
)
relations = data.frame(relations)
“关系”数据框如下所示:
from to
1 3 10
2 10 2
3 2 8
4 8 6
5 6 9
6 9 1
7 1 7
8 7 5
9 5 4
10 4 3
然后我有一个名为“m”的矩阵(这个矩阵的维度对应于“关系”数据框):
m<-matrix(rnorm(100) , nrow = 10)
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] -1.66794194 -0.6407060 0.07796085 0.7399475 0.6879168 -0.3724388 -0.48378063 0.3104807 0.08473729 -1.2512714
[2,] -0.38022652 -0.8497043 -0.96185663 1.9091036 2.1001089 0.9769734 0.51686204 0.4365235 0.75405379 -0.6111659
[3,] 0.91899661 -1.0241288 -0.07130809 -1.4438932 -1.2870305 -0.3745809 0.36896453 -0.4583653 -0.49929202 -1.1854801
[4,] -0.57534696 0.1176466 1.44455086 0.7017843 0.7877388 1.0527115 -0.21538051 -1.0633261 0.21444531 2.1988103
[5,] 0.60796432 -0.9474746 0.45150405 -0.2621975 0.7690422 -1.0491770 0.06529303 1.2631852 -0.32468591 1.3124130
[6,] -1.61788271 -0.4905574 0.04123292 -1.5721442 0.3322026 -1.2601552 -0.03406725 -0.3496504 0.09458353 -0.2651451
[7,] -0.05556197 -0.2560922 -0.42249683 -1.5146677 -1.0083766 3.2410399 2.12845190 -0.8655129 -0.89536336 0.5431941
[8,] 0.51940720 1.8438620 -2.05324722 -1.6015362 -0.1194526 -0.4168576 -0.74133610 -0.2362796 -1.31080153 -0.4143399
[9,] 0.30115336 -0.6519499 1.13133721 -0.5309065 -0.2803953 0.2982276 -1.09599627 -0.1971759 1.99721338 -0.4762469
[10,] 0.10567619 0.2353866 -1.46064007 -1.4617556 0.5629895 0.6365697 0.03778840 1.1099203 0.60070882 -0.7886028
我想编写一个函数:
选择矩阵“m”中对应于“关系”数据框中每一行的“(从,到)数字”的元素
然后将所有这些元素加在一起
在我的例子中,这看起来像这样:
sums = m[3,10] + m[10,2] + m[2,8] + m[8,6] + m[6,9] + m[9,1] + m[1,7] + m[7,5] + m[5,4] + m[4,3]
sums
[1] -0.8444946
我想也许我可以使用以下“技巧”“引用”矩阵中的正确元素:
sums = m[relations[1,1], relations[1,2]] + m[relations[2,1], relations[2,2]] + m[relations[3,1], relations[3,2]] + m[relations[4,1], relations[4,2]] + m[relations[5,1], relations[5,2]] + m[relations[6,1], relations[6,2]] + m[relations[7,1], relations[7,2]] + m[relations[8,1], relations[8,2]] + m[relations[9,1], relations[9,2]] + m[relations[10,1], relations[10,2]]
Error in m[relations[1, 1], relations[1, 2]] : subscript out of bounds
然后我尝试了这个:
sums = m[as.numeric(relations[1,1]), as.numeric(relations[1,2])] +
m[as.numeric(relations[2,1]), as.numeric(relations[2,2])]
+ m[as.numeric(relations[3,1]), as.numeric(relations[3,2])]
+ m[as.numeric(relations[4,1]), as.numeric(relations[4,2])]
+ m[as.numeric(relations[5,1]), as.numeric(relations[5,2])] +
m[as.numeric(relations[6,1]), as.numeric(relations[6,2])] +
m[as.numeric(relations[7,1]), as.numeric(relations[7,2])] +
m[as.numeric(relations[8,1]), as.numeric(relations[8,2])] +
m[as.numeric(relations[9,1]), as.numeric(relations[9,2])] +
m[as.numeric(relations[10,1]), as.numeric(relations[10,2])]
X9
0.08593303
虽然我想错了,因为
-0.8444946 <> 0.08593303
如何编写一个函数,自动对任何“关系”和“m”执行以下操作?
sums = m[3,10] + m[10,2] + m[2,8] + m[8,6] + m[6,9] + m[9,1] + m[1,7] + m[7,5] + m[5,4] + m[4,3]
谢谢!
您可以使用 [
为矩阵 m
编制索引,而另一个矩阵的列对应于 m
的行和列,就像您的 relations
对象一样。所以我们需要做的就是将 relations
隐藏到 matrix
,用它来索引 m
,然后 sum
全部:
my_sums = function(relations, m) {
r = as.matrix(relations)
if(mode(r) != "numeric") mode(r) = "numeric"
sum(m[r])
}
my_sums(relations, m)
(未经测试,因为您没有共享 m
,但应该可以正常工作。)