R中同一列内的成对比较

问题描述 投票:0回答:1

我有特定的响应变量(生物量),我正在分析从不同论文中检索到的一系列环境条件。

示例数据集:

生物质 条件
P1 10 控制
P1 5 物种1
P1 0 物种1
P2 2 控制
P2 4 物种4
P2 6 物种4
P3 2 控制
P3 2 物种1
P3 8 物种2
P3 6 物种3
P3 7 物种4
P3 2 物种5
P4 3 控制
P4 1 物种6

对于每篇论文,我想具体划分每个物种的生物量结果进行对照。比如P1(paper 1),我想把每个物种的生物量结果除以(/)做对照

示例输出(我在这里使用随机数字结果):

比较 生物质_结果
P1 对照与物种1 5
P2 对照与物种4 4
P3 对照与物种1 2
P3 对照与物种2 6
P3 对照与物种3 7
P3 对照与物种4 3
P3 对照与物种5 2
P4 对照与物种6 1

有什么建议吗?

我尝试使用 tidyverse 但没有成功。

r dataframe tidyverse data-manipulation
1个回答
0
投票

这可能会给出预期的结果

library(dplyr)

df %>% 
  summarize(Biomass_sum = sum(Biomass), .by = c(Paper, Condition)) %>% 
  mutate(Biomass_results = Biomass_sum / Biomass_sum[Condition == "Control"], 
         Condition = paste("Control vs", Condition), .by = Paper) %>% 
  filter(Condition != "Control vs Control") %>% 
  select(-Biomass_sum)
  Paper           Condition Biomass_results
1    P1 Control vs Species1       0.5000000
2    P2 Control vs Species4       5.0000000
3    P3 Control vs Species1       1.0000000
4    P3 Control vs Species2       4.0000000
5    P3 Control vs Species3       3.0000000
6    P3 Control vs Species4       3.5000000
7    P3 Control vs Species5       1.0000000
8    P4 Control vs Species6       0.3333333

数据

df <- structure(list(Paper = c("P1", "P1", "P1", "P2", "P2", "P2", 
"P3", "P3", "P3", "P3", "P3", "P3", "P4", "P4"), Biomass = c(10L, 
5L, 0L, 2L, 4L, 6L, 2L, 2L, 8L, 6L, 7L, 2L, 3L, 1L), Condition = c("Control", 
"Species1", "Species1", "Control", "Species4", "Species4", "Control", 
"Species1", "Species2", "Species3", "Species4", "Species5", "Control", 
"Species6")), class = "data.frame", row.names = c(NA, -14L))
© www.soinside.com 2019 - 2024. All rights reserved.