【问题标题】:How to collapse rows of a frequency table to add their counts in a new column?如何折叠频率表的行以将其计数添加到新列中?
【发布时间】:2018-10-08 20:55:28
【问题描述】:

我有一个带有样本分类的数据框:

 Seq_ID   Family Father   Mother   Sex    Role    Type  
   <chr>     <dbl> <chr>    <chr>    <chr>  <chr>   <chr> 
 1 SSC02219 11000. 0        0        Male   Father  Parent
 2 SSC02217 11000. 0        0        Female Mother  Parent
 3 SSC02254 11000. SSC02219 SSC02217 Male   Proband Child 
 4 SSC02220 11000. SSC02219 SSC02217 Female Sibling Child 
 5 SSC02184 11001. 0        0        Male   Father  Parent
 6 SSC02181 11001. 0        0        Female Mother  Parent
 7 SSC02178 11001. SSC02184 SSC02181 Male   Proband Child 
 8 SSC03092 11002. 0        0        Male   Father  Parent
 9 SSC03078 11002. 0        0        Female Mother  Parent
10 SSC03070 11002. SSC03092 SSC03078 Female Proband Child 

目前,要从 a 转到 b,我必须这样做:

library(tidyverse)
library(janitor)

sample.df %>% tabyl(Role, Sex) %>% 
  adorn_totals(where=c("row", "col") ) %>% 
  as.tibble() %>% select(1,4,3,2) %>%
  # Part 2
  mutate(type=c("parent", "parent", "child", "child", " ")) %>% 
  inner_join(., group_by(., type) %>% 
  summarise(total=sum(Total))) %>% 
  select(5,6,1,2,3,4)

我觉得这是一个非常简单的解决方法。有没有更直接的方法来做 dplyr 的第二部分?

一个

b

【问题讨论】:

  • 什么是tabyl函数?
  • 它来自 janitor 包,给出第一个表 a。
  • 第二部分...从哪里开始?
  • 刚刚添加了一条评论以使其更清晰。
  • 我真的不认为 dplyr 是最好的方法,使用基表、ftable 等确实是为交叉表设计的,而 summarise 则不是。

标签: r dataframe dplyr frequency janitor


【解决方案1】:

这是一个选项。 as.tibble 不是必需的。 mutatecase_when 当您有很多类要分配给“父”或“子”时更易于管理。 inner_join 不是必需的,因为我们可以使用 group_bymutate 来计算 total。最后,我喜欢在使用select 函数时记下列名,因为这样我以后会更容易阅读,但是您当然可以使用列索引,只要您确信列索引会是无论您在管道操作中包含哪些新分析,都不会改变。

library(tidyverse)
library(janitor)

sample.df %>% 
  tabyl(Role, Sex) %>% 
  adorn_totals(where=c("row", "col")) %>% 
  select(Role, Total, Male, Female) %>%
  # Part 2
  mutate(type = case_when(
    Role %in% c("Mother", "Father")      ~"parent",
    Role %in% c("Proband", "Sibling")    ~"child",
    TRUE                                 ~" "
  )) %>% 
  group_by(type) %>% 
  mutate(total = sum(Total)) %>%
  ungroup() %>%
  select(type, total, Role, Total, Male, Female)
# # A tibble: 5 x 6
#   type   total Role    Total  Male Female
#   <chr>  <dbl> <chr>   <dbl> <dbl>  <dbl>
# 1 parent    6. Father     3.    3.     0.
# 2 parent    6. Mother     3.    0.     3.
# 3 child     4. Proband    3.    2.     1.
# 4 child     4. Sibling    1.    0.     1.
# 5 " "      10. Total     10.    5.     5.

数据

library(tidyverse)
library(janitor)

sample.df <- read.table(text = "Seq_ID   Family Father   Mother   Sex    Role    Type  
 1 SSC02219 11000  0        0        Male   Father  Parent
 2 SSC02217 11000  0        0        Female Mother  Parent
 3 SSC02254 11000  SSC02219 SSC02217 Male   Proband Child 
 4 SSC02220 11000  SSC02219 SSC02217 Female Sibling Child 
 5 SSC02184 11001  0        0        Male   Father  Parent
 6 SSC02181 11001  0        0        Female Mother  Parent
 7 SSC02178 11001  SSC02184 SSC02181 Male   Proband Child 
 8 SSC03092 11002  0        0        Male   Father  Parent
 9 SSC03078 11002  0        0        Female Mother  Parent
10 SSC03070 11002  SSC03092 SSC03078 Female Proband Child ",
                        header = TRUE, stringsAsFactors = FALSE)

sample.df <- as_tibble(sample.df)

【讨论】:

    【解决方案2】:

    另一种选择是使用knitr

    library(janitor)
    library(tidyverse)
    library(kableExtra)
    library(knitr)
    
    sample.df %>% 
      tabyl(Role, Sex) %>%
      adorn_totals(where=c("row", "col")) %>%
      # Part 2
      mutate(type=case_when(
        Role %in% c('Father', 'Mother') ~ 'parent',
        Role %in% c('Proband', 'Sibling') ~ 'child',
        TRUE ~ ''
      )) %>%
      group_by(type) %>%
      mutate(total=sum(Total)) %>%
      ungroup() %>%
      kable("html") %>%
      kable_styling(c("striped", "bordered")) %>%
      collapse_rows(columns = c(5,6))
    

    输出为:

    样本数据:

    sample.df  <- structure(list(Seq_ID = c("SSC02219", "SSC02217", "SSC02254", 
    "SSC02220", "SSC02184", "SSC02181", "SSC02178", "SSC03092", "SSC03078", 
    "SSC03070"), Family = c(11000L, 11000L, 11000L, 11000L, 11001L, 
    11001L, 11001L, 11002L, 11002L, 11002L), Father = c("0", "0", 
    "SSC02219", "SSC02219", "0", "0", "SSC02184", "0", "0", "SSC03092"
    ), Mother = c("0", "0", "SSC02217", "SSC02217", "0", "0", "SSC02181", 
    "0", "0", "SSC03078"), Sex = c("Male", "Female", "Male", "Female", 
    "Male", "Female", "Male", "Male", "Female", "Female"), Role = c("Father", 
    "Mother", "Proband", "Sibling", "Father", "Mother", "Proband", 
    "Father", "Mother", "Proband"), Type = c("Parent", "Parent", 
    "Child", "Child", "Parent", "Parent", "Child", "Parent", "Parent", 
    "Child")), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", 
    "9", "10"), class = c("tbl_df", "tbl", "data.frame"))
    

    【讨论】:

    • @gaelgarcia 也许你应该accept the answer如果它帮助你解决了你的问题,那么这个问题可以被认为是关闭
    猜你喜欢
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 2014-01-03
    • 2020-09-03
    • 2011-01-02
    • 1970-01-01
    • 2018-10-31
    • 1970-01-01
    相关资源
    最近更新 更多