【发布时间】:2021-07-02 11:36:39
【问题描述】:
我正在尝试对下面的数据进行 t 检验,但它返回的错误是:
Error in var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) : Calling var(x) on a factor x is defunct. Use something like 'all(duplicated(x)[-1L])' to test for a constant vector.
library(tidyverse)
library(broom)
food_consumption <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-02-18/food_consumption.csv')
food_consumption %>%
mutate(vegan = if_else(food_category %in% c("Wheat and Wheat Products", "Rice", "Soybeans", "Nuts inc. Peanut Butter"), "Non-Animal Product", "Animal Product")) %>%
select(consumption, co2_emmission, vegan) %>%
pivot_longer(!vegan, names_to = "type", values_to = "value") %>%
mutate(type = as.factor(type),
vegan = as.factor(vegan)) %>%
group_by(type) %>%
do(test = t.test(value~vegan, data = (.))) %>%
tidy(test)
有人知道这里发生了什么吗?以及如何无误地整理 t 检验输出?如果我在末尾排除 tidy(test) 位,则 t 检验会按预期返回两个列表对象,但如果我尝试调用 tidy() 它会返回上述错误。
我正在关注一个运行完全相同代码的教程(除了它使用 gather 而不是 pivot_wider 但两者都产生相同的数据集)。 Timestamped link here.
【问题讨论】: