[英]How do I perform calculations using the columns of a data table from an ANOVA using the dplyr and rstatix packages?
我有一個數據框,並在數據之間進行了方差分析。 在方差分析之后,我想使用其中一個結果列進行計算並使用mutate()
function 創建一個新列。 但是,會出現一個錯誤,表明無法在 anova anova
object 上執行此操作:
Error: `x` must be a vector, not a <anova_test/data.frame/rstatix_test> object.
有人可以幫我用方差分析結果的 F 列執行計算( F + 1
)嗎?
library(dplyr)
library(rstatix)
Temperature <- factor(c(rep("cold", times = 4),
rep("hot", times = 4)),
levels = c("cold", "hot"))
Light <- factor(rep(c(rep("blue", times = 2),
rep("yellow", times = 2)),
times = 2),
levels = c("blue", "yellow"))
Result <- c(90.40, 85.20, 21.70, 25.30,
75.12, 77.36, 6.11, 10.8)
Data <- data.frame(Temperature, Light, Result)
NewColumn <- Data %>%
anova_test(formula = Result ~ Temperature*Light) %>%
mutate(New= `F` + 1) #<-------- Not working
正如 JKupzig 在評論中提到的,這是dplyr
中的一個已知問題,如下所述: https://github.com/tidyverse/dplyr/issues/5286 。
The issue is caused by anova_test()
creating an output data frame with classes anova_test
, data.frame
and rstatix_test
, in that order, while mutate()
from dplyr
seems to get hung up if the last element in the class vector is not data.frame
。 您可以驗證 anova 的 output 的類如下:
Data %>% anova_test(formula = Result ~ Temperature*Light) %>% class()
[1] "anova_test" "data.frame" "rstatix_test"
作為一種解決方法,您可以在 anova_test() 之后將anova_test()
as_tibble()
添加到dplyr
pipe 中。 這會按順序將類重置為tbl_df
、 tbl
和data.frame
。
Data %>% anova_test(formula = Result ~ Temperature*Light) %>% as_tibble() %>% class()
[1] "tbl_df" "tbl" "data.frame"
Data %>%
anova_test(formula = Result ~ Temperature*Light) %>%
as_tibble() %>%
mutate(New= `F` + 1)
# A tibble: 3 x 8
Effect DFn DFd F p `p<.05` ges New
<chr> <dbl> <dbl> <dbl> <dbl> <chr> <dbl> <dbl>
1 Temperature 1 4 42.2 0.003 "*" 0.914 43.2
2 Light 1 4 1041. 0.0000055 "*" 0.996 1042.
3 Temperature:Light 1 4 0.725 0.442 "" 0.153 1.72
請注意,此操作會刪除anova_test
和rstatix_test
類。 如果這些類很重要,請使用與 magrittr package 不同的解決方法set_class()
( magrittr
是magrittr
的依賴dplyr
,因此無需單獨安裝)。
Data %>%
anova_test(formula = Result ~ Temperature*Light) %>%
magrittr::set_class(c("anova_test", "rstatix_test", "data.frame")) %>%
class()
[1] "anova_test" "rstatix_test" "data.frame"
Data %>%
anova_test(formula = Result ~ Temperature*Light) %>%
magrittr::set_class(c("anova_test", "rstatix_test", "data.frame")) %>%
mutate(New = `F` + 1)
ANOVA Table (type II tests)
Effect DFn DFd F p p<.05 ges New
1 Temperature 1 4 42.250 3.00e-03 * 0.914 43.250
2 Light 1 4 1041.366 5.50e-06 * 0.996 1042.366
3 Temperature:Light 1 4 0.725 4.42e-01 0.153 1.725
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.