correlation between columns by group

后端 未结 3 1930
暗喜
暗喜 2021-01-27 17:06

How do I calculate correlations between one column and all other columns in a data frame in R without using column names? I tried to use ddply and it works if I use just two col

3条回答
  •  离开以前
    2021-01-27 17:29

    You can do this with dplyr using

    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
    

提交回复
热议问题