R sapply函数ifelse变量比较(R sapply function ifelse variable comparison)

编程入门 行业动态 更新时间:2024-10-09 04:17:08
R sapply函数ifelse变量比较(R sapply function ifelse variable comparison)

我正在尝试使用function(x,y) sapply循环遍历我的矩阵。

test<-sapply(colnames(my_mat), function(x, y) ifelse (x==y,-1,cor(test_mat[,x],test_mat[,y], use="p")) )

基本上它计算每个列单元格之间的相关性,但是当它们都是同一个对象时,我希望它返回-1而不是1.我的矩阵设置如下:

A B C A . . . B . . . C . . .

而且我希望对角线上的-1。

我得到一个错误,说Error in x == y : 'y' is missing但是如果我删除了ifelse ,代码工作正常(尽管在对角线中有1而不是-1)。 有任何想法吗?

谢谢。

I'm trying to use sapply with function(x,y) to loop over my matrices..

test<-sapply(colnames(my_mat), function(x, y) ifelse (x==y,-1,cor(test_mat[,x],test_mat[,y], use="p")) )

Basically it calculates the correlation between each columns cells, but when they are both the same object, I want it to return -1 instead of 1. My matrix is set up like so:

A B C A . . . B . . . C . . .

And I want the -1 on the diagonals.

I get an error saying Error in x == y : 'y' is missing but if I remove the ifelse, the code works fine (albeit has 1 in diagonals as opposed to -1). Any ideas?

Thanks.

最满意答案

cor是矢量化的并且有一个矩阵方法,所以要实现这一点,你真正需要做的就是计算矩阵与自身的相关性,这给出了每列之间的相关性。 注意矩阵是对称的。 然后只需使用diag将对角线元素设置为-1 ...

set.seed(1) m <- matrix( rnorm(25) , 5 ) # [,1] [,2] [,3] [,4] [,5] #[1,] -0.6264538 -0.8204684 1.5117812 -0.04493361 0.91897737 #[2,] 0.1836433 0.4874291 0.3898432 -0.01619026 0.78213630 #[3,] -0.8356286 0.7383247 -0.6212406 0.94383621 0.07456498 #[4,] 1.5952808 0.5757814 -2.2146999 0.82122120 -1.98935170 #[5,] 0.3295078 -0.3053884 1.1249309 0.59390132 0.61982575 cor.mat <- cor( m ) diag(cor.mat) <- -1 # [,1] [,2] [,3] [,4] [,5] #[1,] -1.0000000 0.2789049 -0.6149659 0.2491625 -0.7615458 #[2,] 0.2789049 -1.0000000 -0.7864121 0.5683647 -0.5313192 #[3,] -0.6149659 -0.7864121 -1.0000000 -0.6813702 0.9353307 #[4,] 0.2491625 0.5683647 -0.6813702 -1.0000000 -0.6429515 #[5,] -0.7615458 -0.5313192 0.9353307 -0.6429515 -1.0000000

要验证这是否正确,请检查第一列与所有其他列的相关性。 您将看到返回的向量与上面第一列cor.mat相同。

cor( m[,1] , m ) # [,1] [,2] [,3] [,4] [,5] #[1,] 1 0.2789049 -0.6149659 0.2491625 -0.7615458

cor is vectorised and has a method for matrices, so to acheive this all you really need to do is to compute the correlation of the matrix with itself, which gives the correlation between each column. Notice the matrix is symmetrical. Then just use diag to set the diagonal elements to -1...

set.seed(1) m <- matrix( rnorm(25) , 5 ) # [,1] [,2] [,3] [,4] [,5] #[1,] -0.6264538 -0.8204684 1.5117812 -0.04493361 0.91897737 #[2,] 0.1836433 0.4874291 0.3898432 -0.01619026 0.78213630 #[3,] -0.8356286 0.7383247 -0.6212406 0.94383621 0.07456498 #[4,] 1.5952808 0.5757814 -2.2146999 0.82122120 -1.98935170 #[5,] 0.3295078 -0.3053884 1.1249309 0.59390132 0.61982575 cor.mat <- cor( m ) diag(cor.mat) <- -1 # [,1] [,2] [,3] [,4] [,5] #[1,] -1.0000000 0.2789049 -0.6149659 0.2491625 -0.7615458 #[2,] 0.2789049 -1.0000000 -0.7864121 0.5683647 -0.5313192 #[3,] -0.6149659 -0.7864121 -1.0000000 -0.6813702 0.9353307 #[4,] 0.2491625 0.5683647 -0.6813702 -1.0000000 -0.6429515 #[5,] -0.7615458 -0.5313192 0.9353307 -0.6429515 -1.0000000

To verify this is correct, check the correlation of the first column with all the other. You will see that the vector returned is the same as the first column of cor.mat above.

cor( m[,1] , m ) # [,1] [,2] [,3] [,4] [,5] #[1,] 1 0.2789049 -0.6149659 0.2491625 -0.7615458

更多推荐

本文发布于:2023-07-09 10:02:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1085515.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   函数   ifelse   sapply   variable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!