计算多个因素的比例
我有一个看起来像这样的数据框架:
structure(list(Cell.Class = c("Excitatory Neurons", "Inhibitory",
"OPCs", "Medium Spiny Neurons", "Excitatory Neurons", "Inhibitory",
"OPCs", "Medium Spiny Neurons", "Excitatory Neurons", "Inhibitory",
"OPCs", "Medium Spiny Neurons"), Mean.Enrichment = c(2.8, 3,
0.4, 0.42, 18, 2.1, 0.8, 2.8, 0.3, 1, 0, 0), Disorder = c("Bipolar Disorder",
"Bipolar Disorder", "Bipolar Disorder", "Bipolar Disorder", "Schizophrenia",
"Schizophrenia", "Schizophrenia", "Schizophrenia", "Major Depression",
"Major Depression", "Major Depression", "Major Depression")), class = "data.frame", row.names = c(NA,
-12L))
> enrichment.means
Cell.Class Mean.Enrichment Disorder
1 Excitatory Neurons 2.80 Bipolar Disorder
2 Inhibitory 3.00 Bipolar Disorder
3 OPCs 0.40 Bipolar Disorder
4 Medium Spiny Neurons 0.42 Bipolar Disorder
5 Excitatory Neurons 18.00 Schizophrenia
6 Inhibitory 2.10 Schizophrenia
7 OPCs 0.80 Schizophrenia
8 Medium Spiny Neurons 2.80 Schizophrenia
9 Excitatory Neurons 0.30 Major Depression
10 Inhibitory 1.00 Major Depression
11 OPCs 0.00 Major Depression
12 Medium Spiny Neurons 0.00 Major Depression
我想计算每个cell类
都有mean.enrichment
的比例。我尝试了以下操作:
group_by(Disorder) %>%
mutate(Sum= sum(Mean.Enrichment)) %>%
group_by(Disorder, .add=TRUE) %>%
summarise(Proportion = Mean.Enrichment/Sum)
结果
Disorder Proportion
<chr> <dbl>
1 Bipolar Disorder 0.423
2 Bipolar Disorder 0.453
3 Bipolar Disorder 0.0604
4 Bipolar Disorder 0.0634
5 Major Depression 0.231
6 Major Depression 0.769
7 Major Depression 0
8 Major Depression 0
9 Schizophrenia 0.759
10 Schizophrenia 0.0886
11 Schizophrenia 0.0338
12 Schizophrenia 0.118
并没有告诉我哪个cell.class
对应于这里的计算。我该如何实现?
I have a dataframe that looks like this:
structure(list(Cell.Class = c("Excitatory Neurons", "Inhibitory",
"OPCs", "Medium Spiny Neurons", "Excitatory Neurons", "Inhibitory",
"OPCs", "Medium Spiny Neurons", "Excitatory Neurons", "Inhibitory",
"OPCs", "Medium Spiny Neurons"), Mean.Enrichment = c(2.8, 3,
0.4, 0.42, 18, 2.1, 0.8, 2.8, 0.3, 1, 0, 0), Disorder = c("Bipolar Disorder",
"Bipolar Disorder", "Bipolar Disorder", "Bipolar Disorder", "Schizophrenia",
"Schizophrenia", "Schizophrenia", "Schizophrenia", "Major Depression",
"Major Depression", "Major Depression", "Major Depression")), class = "data.frame", row.names = c(NA,
-12L))
> enrichment.means
Cell.Class Mean.Enrichment Disorder
1 Excitatory Neurons 2.80 Bipolar Disorder
2 Inhibitory 3.00 Bipolar Disorder
3 OPCs 0.40 Bipolar Disorder
4 Medium Spiny Neurons 0.42 Bipolar Disorder
5 Excitatory Neurons 18.00 Schizophrenia
6 Inhibitory 2.10 Schizophrenia
7 OPCs 0.80 Schizophrenia
8 Medium Spiny Neurons 2.80 Schizophrenia
9 Excitatory Neurons 0.30 Major Depression
10 Inhibitory 1.00 Major Depression
11 OPCs 0.00 Major Depression
12 Medium Spiny Neurons 0.00 Major Depression
I want to calculate the proportion of Mean.Enrichment
that each Cell Class
has, for each Disorder
. I tried the following:
group_by(Disorder) %>%
mutate(Sum= sum(Mean.Enrichment)) %>%
group_by(Disorder, .add=TRUE) %>%
summarise(Proportion = Mean.Enrichment/Sum)
And the result
Disorder Proportion
<chr> <dbl>
1 Bipolar Disorder 0.423
2 Bipolar Disorder 0.453
3 Bipolar Disorder 0.0604
4 Bipolar Disorder 0.0634
5 Major Depression 0.231
6 Major Depression 0.769
7 Major Depression 0
8 Major Depression 0
9 Schizophrenia 0.759
10 Schizophrenia 0.0886
11 Schizophrenia 0.0338
12 Schizophrenia 0.118
But it's not telling me which Cell.Class
corresponds to which calculation here. How can I achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我明白。
也许是。
如果可能的话,添加了预期的输出。
If I understand.
Maybe it.
If possible add an expected output.
我的解决方案 - 跨(所有())使用
明确保留列。
My solution - use
across(everything())
to explicitly keep the columns.