Joe*_*ger 11 r correlation dataframe
我有一组看起来像A和B的大型数据帧:
A <- data.frame(A1=c(1,2,3,4,5),B1=c(6,7,8,9,10),C1=c(11,12,13,14,15 ))
A1 B1 C1
1 1 6 11
2 2 7 12
3 3 8 13
4 4 9 14
5 5 10 15
B <- data.frame(A2=c(6,7,7,10,11),B2=c(2,1,3,8,11),C2=c(1,5,16,7,8))
A2 B2 C2
1 6 2 1
2 7 1 5
3 7 3 16
4 10 8 7
5 11 11 8
Run Code Online (Sandbox Code Playgroud)
我想创建一个向量(C),表示A1和A2,B1和B2以及C1和C2之间的Pearson相关性.在这种情况下,例如,那些相关性是:
[1] 0.95 0.92 0.46
Run Code Online (Sandbox Code Playgroud)
Rol*_*and 13
cor 接受两个data.frames:
A<-data.frame(A1=c(1,2,3,4,5),B1=c(6,7,8,9,10),C1=c(11,12,13,14,15 ))
B<-data.frame(A2=c(6,7,7,10,11),B2=c(2,1,3,8,11),C2=c(1,5,16,7,8))
cor(A,B)
# A2 B2 C2
# A1 0.9481224 0.9190183 0.459588
# B1 0.9481224 0.9190183 0.459588
# C1 0.9481224 0.9190183 0.459588
diag(cor(A,B))
#[1] 0.9481224 0.9190183 0.4595880
Run Code Online (Sandbox Code Playgroud)
编辑:
以下是一些基准测试:
Unit: microseconds
expr min lq median uq max neval
diag(cor(A, B)) 230.292 238.4225 243.0115 255.0295 352.955 100
mapply(cor, A, B) 267.076 281.5120 286.8030 299.5260 375.087 100
unlist(Map(cor, A, B)) 250.053 259.1045 264.5635 275.9035 1146.140 100
Run Code Online (Sandbox Code Playgroud)
EDIT2:
还有一些更好的基准测试
set.seed(42)
A <- as.data.frame(matrix(rnorm(10*n),ncol=n))
B <- as.data.frame(matrix(rnorm(10*n),ncol=n))
Run Code Online (Sandbox Code Playgroud)

但是,我应该提一下,这些基准测试很大程度上取决于行数.
编辑3:因为我被要求提供基准测试代码,所以在这里.
b <- sapply(2^(1:12), function(n) {
set.seed(42)
A <- as.data.frame(matrix(rnorm(10*n),ncol=n))
B <- as.data.frame(matrix(rnorm(10*n),ncol=n))
require(microbenchmark)
res <- print(microbenchmark(
diag(cor(A,B)),
mapply(cor, A, B),
unlist(Map(cor,A,B)),
times=10
),unit="us")
res$median
})
b <- t(b)
matplot(x=1:12,log10(b),type="l",
ylab="log10(median [µs])",
xlab="log2(n)",col=1:3,lty=1)
legend("topleft", legend=c("diag(cor(A, B))",
"mapply(cor, A, B)",
"unlist(Map(cor,A,B))"),lty=1, col=1:3)
Run Code Online (Sandbox Code Playgroud)
你可以使用apply函数的朋友Map.
Map(function(x,y) cor(x,y),A,B)
$A1
[1] 0.9481224
$B1
[1] 0.9190183
$C1
[1] 0.459588
Run Code Online (Sandbox Code Playgroud)
如果您希望输出为@Jilber建议的向量:
unlist(Map(function(x,y) cor(x,y),A,B))
A1 B1 C1
0.9481224 0.9190183 0.4595880
Run Code Online (Sandbox Code Playgroud)
或者您可以使用:
unlist(Map(cor,A,B))
A1 B1 C1
0.9481224 0.9190183 0.459588
Run Code Online (Sandbox Code Playgroud)
您可以使用另一种替代mapply功能
> mapply(function(x,y) cor(x,y),A,B)
A1 B1 C1
0.9481224 0.9190183 0.4595880
Run Code Online (Sandbox Code Playgroud)
或者就像mapply(cor, A, B)@Aaron所建议的那样.