假設我有
set.seed(1)
e <- data.frame(tti = log2(runif(200)),
corona = c(rep("Corona", 100), rep("Before", 100)),
type = rep(c("A", "B", "C", "D")))
我正在比較COVID-19 之前和期間(大規模tti開始治療的時間)之間的平均差異(這里是我的資料集中更多)。我想為這個重復的任務申請一個,但我對此很陌生,坦率地說,目前我很熟悉。log2e$typen=4for loop
我目前的嘗試:
for(i in unique(e$type)){
m <- c(
round(1-2^(unique(t.test(e$tti[e$type == i] ~ e$corona[e$type == i])$estimate[2]) -
unique(t.test(e$tti[e$type == i] ~ e$corona[e$type == i])$estimate[1])),
digits = 3)*100
)
}
但是,此嘗試僅回傳一個值。
預期輸出:
m應該是一個包含四個平均差估計值的向量
> m
24.7 10.5 1.5 28.7
如何才能做到這一點?
uj5u.com熱心網友回復:
你可以這樣做:
m <- c()
for(i in unique(e$type)){
m[i] <- c(
round(1-2^(unique(t.test(e$tti[e$type == i] ~ e$corona[e$type == i])$estimate[2]) -
unique(t.test(e$tti[e$type == i] ~ e$corona[e$type == i])$estimate[1])),
digits = 3)*100
)
}
這會初始化 m 然后在回圈期間填充它。這應該在一個向量中為您提供所有 4 個結果。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/367551.html
下一篇:自動為資料框串列創建新列-R
