我正在尝试使用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.7615458cor 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.0000000To 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更多推荐
发布评论