我有一个长格式的数据框,显示一组人的重复身高测量值。
平均观察次数计算为 2000/500 = 每个孩子 4 次观察。
如何计算每个孩子的观察次数的中位数和四分位数范围?
data <- data.frame(
child_id = 1:500,
height_1 = rnorm(500, mean = 80, sd = 2),
height_2 = rnorm(500, mean = 90, sd = 2),
height_3 = rnorm(500, mean = 100, sd = 2),
height_4 = rnorm(500, mean = 115, sd = 2)
)
data_long <- reshape(data, varying=c(
"height_1", "height_2", "height_3", "height_4"),
direction= "long", idvar="child_id", timevar = "time", sep="_"
)
# Mean observation per child = 2000/500 = 4
data_long$id_f <- as.factor(data_long$child_id)
length(unique(data_long$id_f)) # 500 children
length(data_long$height) # 2000 observations