[英]How do I perform calculations using the columns of a data table from an ANOVA using the dplyr and rstatix packages?
I have a data frame and have done an ANOVA between the data.我有一个数据框,并在数据之间进行了方差分析。 After the ANOVA I want to use one of the resulting columns to do a calculation and create a new column with the mutate()
function.在方差分析之后,我想使用其中一个结果列进行计算并使用mutate()
function 创建一个新列。 However, an error appears indicating that this operation cannot be done on an anova
class object:但是,会出现一个错误,表明无法在 anova anova
object 上执行此操作:
Error: `x` must be a vector, not a <anova_test/data.frame/rstatix_test> object.
Can someone help me perform calculations ( F + 1
) with the F column of the ANOVA result?有人可以帮我用方差分析结果的 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
As mentioned by JKupzig in the comments, this is a known issue in dplyr
as documented here: https://github.com/tidyverse/dplyr/issues/5286 .正如 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
. 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
。 You can verify the classes of the output of the anova as follows:您可以验证 anova 的 output 的类如下:
Data %>% anova_test(formula = Result ~ Temperature*Light) %>% class()
[1] "anova_test" "data.frame" "rstatix_test"
As a workaround, you can add as_tibble()
to your dplyr
pipe after anova_test()
.作为一种解决方法,您可以在 anova_test() 之后将anova_test()
as_tibble()
添加到dplyr
pipe 中。 This resets the classes to tbl_df
, tbl
, and data.frame
, in that order.这会按顺序将类重置为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
Note, that this action removes the classes anova_test
and rstatix_test
.请注意,此操作会删除anova_test
和rstatix_test
类。 If these classes are important down the line, use a different workaround with set_class()
from the magrittr
package ( magrittr
is a dependency of dplyr
, so no need to install it separately).如果这些类很重要,请使用与 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.