我有一个类似于圣诞的数据集
Christmas <- data_frame(month = c("1", "1", "2", "2"),
NP = c(2, 3, 3, 1),
ND = c(4, 2, 0, 6),
NO = c(1, 5, 2, 4),
variable = c("mean", "sd", "mean", "sd"))我想按月计算每一列的t统计量。我想使用的t-统计量的公式是t-统计=平均值/sd。(注意事项:我想计算所有列(在本例中,它们仅为NP、ND和NO)列)。
新的数据集将类似于t_statistics。
t_statistic <- data_frame(
month = c("1", "2"),
NP = c(2/3, 3),
ND = c(4/2, 0),
NO = c(1/5, 2/4)
)有线索吗?
发布于 2021-12-06 17:47:21
如果我们已经创建了mean/sd值,那么它就是被last除以的first元素(因为每个组只有两行)
library(dplyr)
out <- Christmas %>%
group_by(month) %>%
summarise(across(NP:NO, ~first(.)/last(.)))-output
out
# A tibble: 2 × 4
month NP ND NO
<chr> <dbl> <dbl> <dbl>
1 1 0.667 2 0.2
2 2 3 0 0.5带OP输出的-checking
> identical(t_statistic, out)
[1] TRUE或者如果mean/sd没有被命令
Christmas %>%
arrange(month, variable) %>%
group_by(month) %>%
summarise(across(NP:NO, ~first(.)/last(.)))https://stackoverflow.com/questions/70249652
复制相似问题