我想计算数据集的均值标准误 (SEM)。但是,各组之间的样本大小不同(WT 为 n=8,KO 为 n=9),因此我需要一个条件公式。这是我的数据的一个最小示例:
Experiment <- c("A", "C", "D")
Group <- c("KO", "WT", "KO")
Mean <- c(23, 41, 32)
sd <- c(2, 3, 4)
df <- data.frame(Experiment, Group, Mean, sd)
df
产量:
Experiment Group Mean sd
1 A KO 23 2
2 C WT 41 3
3 D KO 32 4
我试过了
df$SEM <- if (df$Group == "KO") {
df$sd /sqrt(9)
} else {
df$sd /sqrt(8)
}
但这会计算所有组的 SEM 为 9。
答案 0 :(得分:1)
@akrun 在评论中的建议(使用 ifelse
)是一个很好的建议,但我鼓励您更明确一点,并首先将样本大小添加到数据集中:
df <- transform(df, sampsize = ifelse(Group=="KO", 9, 8)
df <- transform(df, SEM = sd/sqrt(sampsize))
您也可以通过在整个 (df$
) 前加上 df$sampsize <- ifelse(df$Group=="KO", 9, 8); etc.
或在 dplyr::mutate()
前加上 library(dplyr)
df <- mutate(df,
sampsize=ifelse(Group=="KO", 9, 8),
sem = sd/sqrt(sampsize)
)
来实现:
import tkinter as tk
def loop():
global label
# If the label exists, destroy it
if label is not None:
label.destroy()
# Create a label
label = tk.Label(root)
label.pack() # You don't even need this line for the error
# After some time call `loop` again
label.after(100, loop)
label = None
root = tk.Tk()
# Start the loop
loop()
root.mainloop()
这样,当您在几个月后回来阅读您的代码时,会更明显地看到发生了什么(您可能立即认识到 SEM 的公式,但您可能不会...... )