【问题标题】:long to wide format aggregate R tidyverse [duplicate]长到宽格式聚合R tidyverse [重复]
【发布时间】:2019-12-13 13:55:54
【问题描述】:

您好,给定以下数据框

library(tidyverse)

df <- data.frame(READS=rep(c('READa', 'READb', 'READc'),each=3) ,GENE=rep(c('GENEa', 'GENEb', 'GENEc'), each=3), COMMENT=rep(c('CommentA', 'CommentA', 'CommentA'),each=3))
> df
  READS  GENE  COMMENT
1 READa GENEa CommentA
2 READa GENEa CommentA
3 READa GENEa CommentA
4 READb GENEb CommentA
5 READb GENEb CommentA
6 READb GENEb CommentA
7 READc GENEc CommentA
8 READc GENEc CommentA
9 READc GENEc CommentA

我想通过基因列将长格式聚合转换为宽格式,以便获得以下信息

         GENEa   GENEb  GENEc
READSa     3        3     3 
READSb     3        3     3

我试过没有成功:

 library(tidyverse)
      df %>% 
      group_by(GENE) %>% 
      select(-COMMENT) %>%
      spread(READS) 

请注意,原始数据框很大,因此任何优化的代码都会有所帮助。

感谢您的帮助。

【问题讨论】:

  • 为什么READsaGENEc3?不存在这样的组合。

标签: r tidyverse


【解决方案1】:

不太确定如何获得 GENEaREADSb 的 3 个计数,但假设您想要计数,您可以尝试以下操作:


library(tidyverse)

df <- tibble(
  READS = rep(c("READa", "READb", "READc"), each = 3), 
  GENE = rep(c("GENEa", "GENEb", "GENEc"), each = 3), 
  COMMENT = rep(c("CommentA", "CommentA", "CommentA"), each = 3)
)
df
#> # A tibble: 9 x 3
#>   READS GENE  COMMENT 
#>   <chr> <chr> <chr>   
#> 1 READa GENEa CommentA
#> 2 READa GENEa CommentA
#> 3 READa GENEa CommentA
#> 4 READb GENEb CommentA
#> 5 READb GENEb CommentA
#> 6 READb GENEb CommentA
#> 7 READc GENEc CommentA
#> 8 READc GENEc CommentA
#> 9 READc GENEc CommentA

df %>%
  count(READS, GENE) %>%
  pivot_wider(
    names_from = GENE, values_from = n,
    values_fill = list(n = 0)
  )
#> # A tibble: 3 x 4
#>   READS GENEa GENEb GENEc
#>   <chr> <int> <int> <int>
#> 1 READa     3     0     0
#> 2 READb     0     3     0
#> 3 READc     0     0     3

reprex package (v0.3.0) 于 2019 年 12 月 13 日创建

【讨论】:

    【解决方案2】:

    假设您希望每个输出单元格中的数字是输入中具有该单元格的行和列名称的行数,那么这是基本 R 中的单行。

    table(df[1:2])
    

    给出这个table 类对象:

           GENE
    READS   GENEa GENEb GENEc
      READa     3     0     0
      READb     0     3     0
      READc     0     0     3
    

    如果您希望将结果作为数据框,则:

    as.data.frame.matrix(table(df[1:2]))
    

    【讨论】:

      【解决方案3】:
      library(tidyr) #v1.0.0
      pivot_wider(df, -COMMENT, names_from = GENE, values_from = GENE, 
                                values_fn = list(GENE = length), values_fill = list(GENE=0))
      
      # A tibble: 3 x 4
        READS GENEa GENEb GENEc
        <fct> <int> <int> <int>
      1 READa     3     0     0
      2 READb     0     3     0
      3 READc     0     0     3
      

      【讨论】:

        【解决方案4】:

        dcast 的选项

        library(data.table)
        dcast(setDT(df), READS ~ GENE, length)
        #   READS GENEa GENEb GENEc
        #1: READa     3     0     0
        #2: READb     0     3     0
        #3: READc     0     0     3
        

        【讨论】:

          【解决方案5】:

          鉴于您想要的输出的某些组合不存在:

          df <- data.frame(READS=rep(c('READa', 'READb', 'READc'),each=3) ,GENE=rep(c('GENEa', 'GENEb', 'GENEc'), each=3), COMMENT=rep(c('CommentA', 'CommentA', 'CommentA'),each=3))
          
          df %>%
            group_by(READS, GENE) %>% 
            summarise(count = n()) %>% 
            spread(key = "GENE", value = "count") 
          

          会导致

            READS GENEa GENEb GENEc
          1 READa     3    NA    NA
          2 READb    NA     3    NA
          3 READc    NA    NA     3
          

          请注意,spread 已被弃用,在新版本中您应该使用 pivot_wider。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-05-18
            • 1970-01-01
            • 2014-06-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-06-07
            相关资源
            最近更新 更多