我有一个包含 3 组和 3 天的数据框:
set.seed(10)
dat <- data.frame(group=rep(c("g1","g2","g3"),each=3), day=rep(c(0,2,4),3), value=runif(9))
# group day value
# 1 g1 0 0.507478
# 2 g1 2 0.306769
# 3 g1 4 0.426908
# 4 g2 0 0.693102
# 5 g2 2 0.085136
# 6 g2 4 0.225437
# 7 g3 0 0.274531
# 8 g3 2 0.272305
# 9 g3 4 0.615829
我想取 log2 并将每个值除以每组中的第 0 天值。我现在这样做的方式是在中间步骤中计算每一天的组:
day_0 <- dat[dat$day==0, "value"]
day_2 <- dat[dat$day==2, "value"]
day_4 <- dat[dat$day==4, "value"]
res <- cbind(0, log2(day_2/day_0), log2(day_4/day_0))
rownames(res) <- c("g1","g2","g3")
colnames(res) <- c("day_0","log_ratio_day_2_day_0","log_ratio_day_4_day_0")
# day_0 log_ratio_day_2_day_0 log_ratio_day_4_day_0
# g1 0 -0.7261955 -0.249422
# g2 0 -3.0252272 -1.620346
# g3 0 -0.0117427 1.165564
res
没有中间步骤的正确计算方法是什么?