我有这样的数据;
df <- data.frame(concentration=c(0,0,0,0,2,2,2,2,4,4,6,6,6),
result=c(0,0,0,0,0,0,1,0,1,0,1,1,1))我要计算每个浓度水平的结果总数。我想数数每个浓度水平的阳性样本的数量。我想要创建一个新的数据,集中水平,总结果,和积极的数字。
conc pos_c total_c
0 0 4
2 1 4
4 1 2
6 3 3这就是我到目前为止使用plyr的想法;
c <- count(df, "concentration")
r <- count(df, "concentration","result")
names(c)[which(names(c) == "freq")] <- "total_c"
names(r)[which(names(r) == "freq")] <- "pos_c"
cbind(c,r)
concentration total_c concentration pos_c
1 0 4 0 0
2 2 4 2 1
3 4 2 4 1
4 6 3 6 3重复浓缩柱我想也许有更好/更容易的方法来做这件事,我错过了。也许是另一个图书馆。我不知道如何在R中这样做,这对我来说还是比较新的。谢谢。
发布于 2018-07-06 18:30:23
我们需要一个由sum组织的小组。使用tidyverse,我们按‘集中度(group_by)分组,然后由summarise获得逻辑表达式(result > 0)的两列- 1) sum,2)行数(n())。
library(dplyr)
df %>%
group_by(conc = concentration) %>%
summarise(pos_c = sum(result > 0), # in the example just sum(result)
total_c = n())
# A tibble: 4 x 3
# conc pos_c total_c
# <dbl> <int> <int>
#1 0 0 4
#2 2 1 4
#3 4 1 2
#4 6 3 3或将base R与table和addmargins结合使用
addmargins(table(df), 2)[,-1]https://stackoverflow.com/questions/51215958
复制相似问题