給出一個數據幀在數據幀計算各行
ID days dose1 dose2 dose3 dose4 pattern
1 TM 2 11.0 45 0.2 0.1 spots
2 ZZ 18 2.0 6 8.0 0.0 no spots
3 YY 5 0.4 8 10.0 20.0 no spots
4 GG 5 0.4 8 10.0 20.0 spots
df <- structure(list(ID = c("TM", "ZZ", "YY", "GG"), days = c(2L, 18L,
5L, 5L), dose1 = c(11, 2, 0.4, 0.4), dose2 = c(45L, 6L, 8L, 8L
), dose3 = c(0.2, 8, 10, 10), dose4 = c(0.1, 0, 20, 20), pattern = c("spots",
"no spots", "no spots", "spots")), .Names = c("ID", "days", "dose1",
"dose2", "dose3", "dose4", "pattern"), row.names = c(NA, -4L), class = "data.frame")
library(data.table)
setDT(df)
我想下面給出計算每個行,並將其概括圖案「點」和「無點」 -
dfx <- df[, list(
Cal1 = sum(dose1>0)/days,
Cal2 = sum(dose2>0)/days,
Cal3 = sum(dose3>0)/days,
Cal4 = sum(dose4>0)/days
), by=pattern]
無論如何,我可以像上面那樣計算每行並將其添加到數據幀dfx中嗎?
這不是完全清楚你要計算一下:預期輸出的指示,將有助於。 – neilfws