1)由這裏是一個基礎R溶液:
do.call("rbind", by(DF, DF$group_ID, function(d)
data.frame(d, sapply(d[3:5], function(x) x/median(x[d$class == "foo"])))
))
,並提供:
group_ID class var1 var2 var3 metadata var1.1 var2.1 var3.1
a.1 a foo 1.0 324 3 cat 1.0000000 1.0000000 1.00000000
a.2 a bar 1.3 34 53 dog 1.3000000 0.1049383 17.66666667
a.3 a baz 31.0 34 5 elephant 31.0000000 0.1049383 1.66666667
b.4 b foo 34.0 34 943 dolphin 1.0000000 1.0000000 1.00000000
b.5 b bar 94.0 51 23 chipmunk 2.7647059 1.5000000 0.02439024
b.6 b baz 985.0 595 43 badger 28.9705882 17.5000000 0.04559915
c.7 c foo 43.0 93 23 tapir 1.0000000 1.0000000 1.00000000
c.8 c bar 43.0 23 23 monkey 1.0000000 0.2473118 1.00000000
c.9 c baz 40.0 53 512 duck 0.9302326 0.5698925 22.26086957
2)/掃使用sweep
的替代,並再次只有基本功能是:
do.call("rbind", by(DF, DF$group_ID, function(d) {
med <- apply(subset(d, class == "foo")[3:5], 2, median)
data.frame(d, sweep(as.matrix(d[3:5]), 2, med, "/"))
}))
3)sapply/AVE又一鹼溶液是應用ave
到每個var
列組成:
data.frame(DF, sapply(names(DF[3:5]), function(j)
ave(1:nrow(DF), DF$group_ID, FUN = function(i)
DF[i, j]/median(subset(DF[i, ], class == "foo")[[j]]))
))
注:在重現的形式輸入DF
是:
DF <- structure(list(group_ID = structure(c(1L, 1L, 1L, 2L, 2L, 2L,
3L, 3L, 3L), .Label = c("a", "b", "c"), class = "factor"), class = structure(c(3L,
1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L), .Label = c("bar", "baz", "foo"
), class = "factor"), var1 = c(1, 1.3, 31, 34, 94, 985, 43, 43,
40), var2 = c(324L, 34L, 34L, 34L, 51L, 595L, 93L, 23L, 53L),
var3 = c(3L, 53L, 5L, 943L, 23L, 43L, 23L, 23L, 512L), metadata = structure(c(2L,
4L, 7L, 5L, 3L, 1L, 9L, 8L, 6L), .Label = c("badger", "cat",
"chipmunk", "dog", "dolphin", "duck", "elephant", "monkey",
"tapir"), class = "factor")), .Names = c("group_ID", "class",
"var1", "var2", "var3", "metadata"), class = "data.frame", row.names = c(NA,
-9L))
這適用於我,但您應該添加此示例的預期結果,以確保'df%>%group_by(group_ID)%>% mutate_每個(funs(./中位數(。[class ==「bar」])),var1:var3)' –