基于 R 中的 apply 函数创建矩阵
我在 R 中有一个像这样的矩阵
dat <- matrix(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1), ncol = 4)
,我想计算互信息的局部变量 (lv)。对于一对列,我可以使用以下代码来查找 lv ,
dat <- dat[,c(1,2)]
#install.packages("rinform")
library(rinform)
library(devtools)
#install_github("ELIFE-ASU/rinform")
#install_github("ELIFE-ASU/rinform", ref = "dev")
library(rinform)
mutual_info(dat)
mi <- mutual_info(dat, local = T)
mi <- as.matrix(mi)
结果是:
> mi
[,1]
[1,] -1.0000000
[2,] -1.0000000
[3,] 0.2223924
[4,] 0.2223924
[5,] 0.2223924
[6,] 0.2223924
[7,] 0.2223924
[8,] 0.2223924
[9,] 0.2223924
[10,] 0.2223924
[11,] 0.2223924
[12,] 0.2223924
[13,] 0.2223924
[14,] 0.2223924
[15,] 0.2223924
[16,] 0.2223924
[17,] 1.5849625
[18,] 1.5849625
[19,] 1.5849625
[20,] -1.5849625
如何创建一个矩阵,其中每列将具有每对的 mi
值?尝试使用 apply 函数但无法使其工作。输出结果必须是一个包含所有成对列组合的20x6
矩阵,即上面的dat[,c(1,2)]
的mi过程,dat[,c(1,3)]
, dat[,c(1,4)]
, dat[,c(2,3)]
>, dat[,c(2,4)]
,dat[,c(3,4)]
。有人可以帮助我吗?
I have a matrix in R like this
dat <- matrix(c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1,
0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1), ncol = 4)
and i want to calculate the local variant (lv) of mutual information. For a pair of columns I can use the following code to find the lv
dat <- dat[,c(1,2)]
#install.packages("rinform")
library(rinform)
library(devtools)
#install_github("ELIFE-ASU/rinform")
#install_github("ELIFE-ASU/rinform", ref = "dev")
library(rinform)
mutual_info(dat)
mi <- mutual_info(dat, local = T)
mi <- as.matrix(mi)
which results in:
> mi
[,1]
[1,] -1.0000000
[2,] -1.0000000
[3,] 0.2223924
[4,] 0.2223924
[5,] 0.2223924
[6,] 0.2223924
[7,] 0.2223924
[8,] 0.2223924
[9,] 0.2223924
[10,] 0.2223924
[11,] 0.2223924
[12,] 0.2223924
[13,] 0.2223924
[14,] 0.2223924
[15,] 0.2223924
[16,] 0.2223924
[17,] 1.5849625
[18,] 1.5849625
[19,] 1.5849625
[20,] -1.5849625
How can I create a matrix that each column will have the mi
values of each pair? Tried to use an apply function but couldn't make it work. The output result must be a 20x6
matrix with all pair-wise column combinations, i.e., the above mi procedure for dat[,c(1,2)]
, dat[,c(1,3)]
, dat[,c(1,4)]
, dat[,c(2,3)]
, dat[,c(2,4)]
, dat[,c(3,4)]
. Can someone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法验证这种方法,因为我不知道
rinform
软件包。您可以尝试combn(1:4,2)
从1:4
中创建两个元素的矩阵。应用
函数的输入来生成结果。命名
如果将新数据保存到矩阵中,
则可以使用相同的方法命名列:
I can't verify this approach since I don't know the
rinform
package. You could trycombn(1:4, 2)
creates a matrix of all possible of two elements from1:4
.apply
function to generate the result.Naming
If you save the new data into a matrix
you could name the columns using the same approach: