【发布时间】: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 的第二部分?
【问题讨论】:
-
什么是
tabyl函数? -
它来自 janitor 包,给出第一个表 a。
-
第二部分...从哪里开始?
-
刚刚添加了一条评论以使其更清晰。
-
我真的不认为 dplyr 是最好的方法,使用基表、ftable 等确实是为交叉表设计的,而 summarise 则不是。
标签: r dataframe dplyr frequency janitor