我有跑了幾個回合,像這樣一個實驗的不同治療方法的測量:從組成員組內差異
set.seed(1)
df <- data.frame(treatment = rep(c('baseline', 'treatment 1', 'treatment 2'),
times=5),
round = rep(1:5, each=3),
measurement1 = rep(1:5, each=3) + rnorm(15),
measurement2 = rep(1:5, each=3) + rnorm(15))
df
# treatment round measurement1 measurement2
# 1 baseline 1 0.3735462 0.9550664
# 2 treatment 1 1 1.1836433 0.9838097
# 3 treatment 2 1 0.1643714 1.9438362
# 4 baseline 2 3.5952808 2.8212212
# 5 treatment 1 2 2.3295078 2.5939013
# 6 treatment 2 2 1.1795316 2.9189774
# 7 baseline 3 3.4874291 3.7821363
# 8 treatment 1 3 3.7383247 3.0745650
# 9 treatment 2 3 3.5757814 1.0106483
# 10 baseline 4 3.6946116 4.6198257
# 11 treatment 1 4 5.5117812 3.9438713
# 12 treatment 2 4 4.3898432 3.8442045
# 13 baseline 5 4.3787594 3.5292476
# 14 treatment 1 5 2.7853001 4.5218499
# 15 treatment 2 5 6.1249309 5.4179416
我想是一個data.frame
包含兩次測量的差異每個治療與每輪的基線之間。也就是說,按round
分組,我希望從兩個測量中的每一箇中減去baseline
treatment
中的相應測量值。
如果存在但我會更喜歡dplyr
解決方案,但會接受任何優雅的邊界。
非常友善的你接受'任何接近優雅的東西。「但你有什麼嘗試過自己? – mtoto
'df [,3:4] - df [df $ treatment =='baseline',] [df $ round,3:4]' – rawr