使用以下数据框,我想通过复制和分组对数据进行分组,然后计算处理值与控制值的比率。
structure(list(group = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L,
2L), .Label = c("case", "controls"), class = "factor"), treatment = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "EPA", class = "factor"),
replicate = structure(c(2L, 4L, 3L, 1L, 2L, 4L, 3L, 1L), .Label = c("four",
"one", "three", "two"), class = "factor"), fatty_acid_family = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "saturated", class = "factor"),
fatty_acid = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "14:0", class = "factor"),
quant = c(6.16, 6.415, 4.02, 4.05, 4.62, 4.435, 3.755, 3.755
)), .Names = c("group", "treatment", "replicate", "fatty_acid_family",
"fatty_acid", "quant"), class = "data.frame", row.names = c(NA,
-8L))
我尝试使用 dplyr 如下:
group_by(dataIn, replicate, group) %>% transmute(ratio = quant[group=="case"]/quant[group=="controls"])
但这会导致
Error: incompatible size (%d), expecting %d (the group size) or 1
最初我认为这可能是因为我试图从 8 行深的 df 创建 4 个比率,所以我想
summarise
可能是答案(将每个组折叠成一个比率),但这也不起作用(我的理解是一个缺点)。group_by(dataIn, replicate, group) %>% summarise(ratio = quant[group=="case"]/quant[group=="controls"])
replicate group ratio
1 four case NA
2 four controls NA
3 one case NA
4 one controls NA
5 three case NA
6 three controls NA
7 two case NA
8 two controls NA
我会很感激一些关于我哪里出错的建议,或者即使这可以通过
dplyr
来完成.谢谢。
最佳答案
你可以试试:
group_by(dataIn, replicate) %>%
summarise(ratio = quant[group=="case"]/quant[group=="controls"])
#Source: local data frame [4 x 2]
#
# replicate ratio
#1 four 1.078562
#2 one 1.333333
#3 three 1.070573
#4 two 1.446449
由于您按复制和组分组,因此无法同时访问来自不同组的数据。
关于r - 使用 dplyr 按组计算比率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28487526/