【问题标题】:Correlation matrix of grouped variables in dplyrdplyr中分组变量的相关矩阵
【发布时间】:2015-03-26 03:40:32
【问题描述】:

我有一个包含 50 个数字列的分组数据框(使用 dplyr),这些列使用其中一列分成组。我想计算所有非分组列和一个特定列之间的相关矩阵。

mtcars 数据集的示例:

data(mtcars)
cor(mtcars[,2:11], mtcars[,2])

返回每加仑英里数与其他变量之间的相关性列表。

但是,假设我希望为每组气缸计算相同的相关性,例如:

library(dplyr)
mtcars <-
    mtcars %>%
    group_by(cyl)

我该怎么做?我在想类似的事情

mtcars %>%
    group_by(cyl) %>%
    summarise_each(funs(cor(...))

但我不知道在... 中放入什么,因为我不知道如何在dplyr 链中指定列。

相关Linear model and dplyr - a better solution? 的答案与@akrun 的答案非常相似。此外,交叉验证:https://stats.stackexchange.com/questions/4040/r-compute-correlation-by-group 有其他解决方案使用不是dplyr 的包。

【问题讨论】:

    标签: r dplyr


    【解决方案1】:

    我们可以使用do

    library(dplyr)
    mtcars %>% 
           group_by(cyl) %>%
           do(data.frame(Cor=t(cor(.[,3:11], .[,3]))))
    # A tibble: 3 x 10
    # Groups:   cyl [3]
    #    cyl Cor.disp Cor.hp Cor.drat Cor.wt Cor.qsec Cor.vs Cor.am Cor.gear Cor.carb
    #  <dbl>    <dbl>  <dbl>    <dbl>  <dbl>    <dbl>  <dbl>  <dbl>    <dbl>    <dbl>
    #1     4     1.00  0.435  -0.500   0.857    0.328 -0.187 -0.734  -0.0679   0.490 
    #2     6     1.00 -0.514  -0.831   0.473    0.789  0.637 -0.637  -0.899   -0.942 
    #3     8     1     0.118  -0.0922  0.755    0.195 NA     -0.169  -0.169    0.0615
    

    注意:t 部分由@Alex 贡献


    或使用group_modify

    mtcars %>%
        select(-mpg) %>% 
        group_by(cyl) %>%
        group_modify(.f = ~ as.data.frame(t(cor(select(.x, everything()), 
              .x[['disp']]))))
    # A tibble: 3 x 10
    # Groups:   cyl [3]
    #    cyl  disp     hp    drat    wt  qsec     vs     am    gear    carb
    #  <dbl> <dbl>  <dbl>   <dbl> <dbl> <dbl>  <dbl>  <dbl>   <dbl>   <dbl>
    #1     4  1.00  0.435 -0.500  0.857 0.328 -0.187 -0.734 -0.0679  0.490 
    #2     6  1.00 -0.514 -0.831  0.473 0.789  0.637 -0.637 -0.899  -0.942 
    #3     8  1     0.118 -0.0922 0.755 0.195 NA     -0.169 -0.169   0.0615
    

    或者另一个选项是summariseacross。创建了一个新列 'disp1' 作为 'disp' 然后按 'cyl' 分组,使用 'disp1' 获取列 'disp' 到 'carb' 的 cor

     mtcars %>%
         mutate(disp1 = disp) %>%
         group_by(cyl) %>% 
         summarise(across(disp:carb, ~ cor(., disp1)))
    # A tibble: 3 x 10
    #    cyl  disp     hp    drat    wt  qsec     vs     am    gear    carb
    #* <dbl> <dbl>  <dbl>   <dbl> <dbl> <dbl>  <dbl>  <dbl>   <dbl>   <dbl>
    #1     4  1.00  0.435 -0.500  0.857 0.328 -0.187 -0.734 -0.0679  0.490 
    #2     6  1.00 -0.514 -0.831  0.473 0.789  0.637 -0.637 -0.899  -0.942 
    #3     8  1     0.118 -0.0922 0.755 0.195 NA     -0.169 -0.169   0.0615
    

    或者

    library(data.table)
    d1 <- copy(mtcars)
    setnames(setDT(d1)[, as.list(cor(.SD, .SD[[1]])) , cyl, 
                                .SDcols=3:11],  names(d1)[2:11])[]
    

    【讨论】:

    • 既然dosummerise 取代,新语法应该如何进入dplyr?
    • @PrzemyslawRemin 试试mtcars %&gt;% group_by(cyl) %&gt;% group_modify(.f = ~as.data.frame(t(cor(.x[2:10], .x[[2]]))))
    • 您能否将其包含在您的答案中并解释其工作原理? .f 是什么.x 是什么? t 是什么?
    • @PrzemyslawRemin .f 是应用于每个组的函数,.x 是组元素,即组数据,它是一个匿名函数~ => function(x) x[2: 10]`。我不建议使用索引,因为在分组数据集中,它会排除组
    • 谢谢。知道了。你能用列名写出来吗?像 select(cyl) 而不是 .x[[2]] 之类的东西,还有像 select(-cyl) 之类的 cor ?
    猜你喜欢
    • 1970-01-01
    • 2021-12-13
    • 2015-09-23
    • 1970-01-01
    • 2018-10-31
    • 2017-01-12
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    相关资源
    最近更新 更多