我有一個像Christmas這樣的資料集:
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-statistic 的公式是 t-statistic = mean/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)
)
有什么線索嗎?
uj5u.com熱心網友回復:
如果我們已經mean/sd創建了值,那么它只是first元素除以last(因為每組只有兩行)
library(dplyr)
out <- Christmas %>%
group_by(month) %>%
summarise(across(NP:NO, ~first(.)/last(.)))
-輸出
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 的輸出
> identical(t_statistic, out)
[1] TRUE
或者如果mean/sd沒有訂購
Christmas %>%
arrange(month, variable) %>%
group_by(month) %>%
summarise(across(NP:NO, ~first(.)/last(.)))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/375180.html
