【问题标题】:dataframe (product) correlations in RR中的数据框(产品)相关性
【发布时间】:2013-05-16 07:34:26
【问题描述】:

我有 2 个数据框,每个数据框有 150 行和 10 列 + 列和行 ID。我想将一个数据帧中的每一行与另一个数据帧中的每一行相关联(例如 150x150 相关性)并绘制结果 22500 值的分布。(然后我想从分布中计算 p 值等 - 但这是下一步)。

坦率地说,我不知道从哪里开始。我可以读取我的数据并查看如何关联向量或匹配两个矩阵的切片等,但我无法处理我在这里尝试做的事情。

【问题讨论】:

标签: r dataframe correlation


【解决方案1】:
set.seed(42)
DF1 <- as.data.frame(matrix(rnorm(1500),150))
DF2 <- as.data.frame(matrix(runif(1500),150))

#transform to matrices for better performance
m1 <- as.matrix(DF1)
m2 <- as.matrix(DF2)

#use outer to get all combinations of row numbers and apply a function to them
#22500 combinations is small enough to fit into RAM
cors <- outer(seq_len(nrow(DF1)),seq_len(nrow(DF2)),
     #you need a vectorized function
     #Vectorize takes care of that, but is just a hidden loop (slow for huge row numbers)
     FUN=Vectorize(function(i,j) cor(m1[i,],m2[j,])))
hist(cors)

【讨论】:

  • +1,但是您能否在代码中添加一些描述。这使得它对其他人(包括 OP)更加有用。
  • 嗨,Roland,非常感谢 - 它成功了,我学到了很多东西。干杯。 (哦,也谢谢你,保罗)。
【解决方案2】:

您可以将cor 与两个参数一起使用:

cor( t(m1), t(m2) )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多