【问题标题】:correlation between columns by group按组列之间的相关性
【发布时间】:2017-01-11 19:15:26
【问题描述】:

如何在不使用列名的情况下计算 R 中数据框中的一列与所有其他列之间的相关性? 我尝试使用 ddply,如果我只使用两个列名,它就可以工作,即

library(plyr)
ddply(iris, ~Species, summarize, cormat=cor(Sepal.Length,Petal.Width)) 

但是如何在不使用列名的情况下获得第 1 列与所有其他列的相关性,按物种细分?

【问题讨论】:

    标签: r correlation


    【解决方案1】:

    也许是这样的?它为每个物种生成一个相关矩阵。

    by(iris[,1:4], iris$Species, cor)

    【讨论】:

      【解决方案2】:

      您可以使用 dplyr 来做到这一点

      library(dplyr)
      cormat_res <- iris %>%
         group_by(Species) %>%
         do(cormat = cor(select(., -matches("Species"))))
      
      
      > cormat_res[[2]]
      [[1]]
                   Sepal.Length Sepal.Width Petal.Length Petal.Width
      Sepal.Length    1.0000000   0.7425467    0.2671758   0.2780984
      Sepal.Width     0.7425467   1.0000000    0.1777000   0.2327520
      Petal.Length    0.2671758   0.1777000    1.0000000   0.3316300
      Petal.Width     0.2780984   0.2327520    0.3316300   1.0000000
      
      [[2]]
                   Sepal.Length Sepal.Width Petal.Length Petal.Width
      Sepal.Length    1.0000000   0.5259107    0.7540490   0.5464611
      Sepal.Width     0.5259107   1.0000000    0.5605221   0.6639987
      Petal.Length    0.7540490   0.5605221    1.0000000   0.7866681
      Petal.Width     0.5464611   0.6639987    0.7866681   1.0000000
      
      [[3]]
                   Sepal.Length Sepal.Width Petal.Length Petal.Width
      Sepal.Length    1.0000000   0.4572278    0.8642247   0.2811077
      Sepal.Width     0.4572278   1.0000000    0.4010446   0.5377280
      Petal.Length    0.8642247   0.4010446    1.0000000   0.3221082
      Petal.Width     0.2811077   0.5377280    0.3221082   1.0000000
      

      【讨论】:

      • 如果我只是将其复制到 R 中,则会出现错误。你能再解释一下吗?什么是 %>% ?我对 R 不太擅长。
      • 对不起,我看错了你的问题,在dplyr写了一个解决方案,plyr的更新版本
      【解决方案3】:

      截至

      packageVersion("dplyr")
      [1] ‘1.0.2’
      

      其中一个答案中建议的代码结果返回tibble

      iris %>%
           group_by(Species) %>%
           do(cormat = cor(select(., -matches("Species"))))
      # A tibble: 3 x 2
      # Rowwise: 
        Species    cormat           
        <fct>      <list>           
      1 setosa     <dbl[,4] [4 × 4]>
      2 versicolor <dbl[,4] [4 × 4]>
      3 virginica  <dbl[,4] [4 × 4]>
      

      要将数据变成矩形,可以

      iris_cor <- iris %>%
           group_by(Species) %>%
           do(cormat = cor(select(., -matches("Species")))) %>%
           pull(cormat) %>% melt
      

      您将在 L1 变量上编码物种的级别。

                 Var1         Var2     value L1
      1  Sepal.Length Sepal.Length 1.0000000  1
      2   Sepal.Width Sepal.Length 0.7425467  1
      3  Petal.Length Sepal.Length 0.2671758  1
      4   Petal.Width Sepal.Length 0.2780984  1
      ...
      

      我相信unnest() 和它的朋友们有一种更简洁的方法来做这件事,但还想不通。希望这会引起注意 并发布更好的解决方案

      【讨论】:

        猜你喜欢
        • 2014-02-09
        • 2018-07-01
        • 2019-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-27
        • 2014-03-22
        相关资源
        最近更新 更多