【问题标题】:Drop a Categorical Level <- Dplyr <- Statistics With R删除分类级别 <- Dplyr <- 使用 R 的统计信息
【发布时间】:2018-01-06 09:18:41
【问题描述】:

使用 R 中的 dplyr 包,我正在尝试将分类变量从 3 级变为仅 2 级。我正在使用著名的 iris 数据集并尝试转换类变量(包含:“Iris-versicolor” , "Iris-setosa", & "Iris-virginica") 合二为一,只有两个层次(包含:"Iris-versicolor", "Iris-setosa")。所以,我想创建一个新的数据集,我想出了这个:

IRIS_TEST2 <- IRIS_TEST %>%
   filter(class != "Iris-virginica")

所以,当我尝试对其进行假设检验时:

inference(y = sepal_length, x = class, data = IRIS_TEST2, statistic = "mean", type = 
      "ci", method = "theoretical", conf_level = .95)

我继续收到错误:

Error: Categorical variable has more than 2 levels, confidence interval is undefined,
         use ANOVA to test for a difference between means

或者,我可以使用一种方法来附加“x =”以仅包含“Iris-versicolor”和“Iris-setosa”

inference(y = sepal_length, x = class, data = IRIS_TEST2, statistic = "mean", type = 
        "ci", method = "theoretical", conf_level = .95)

任何帮助将不胜感激!

【问题讨论】:

  • 你考虑过使用droplevels吗?以防万一,请参阅this question
  • 是的 - 你成功了,谢谢!

标签: r dplyr categorical-data


【解决方案1】:

过滤掉我不想要的类(并将其存储到新变量中)后,我能够运行以下代码:

IRIS_TEST2$class <- factor(IRIS_TEST2$class)

这让我只有两个水平,我能够运行我的假设检验并找到置信区间

【讨论】:

  • 我明白了 - 我将 factor() 更改为:drop levels()
猜你喜欢
  • 2021-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-15
  • 1970-01-01
  • 2019-12-30
  • 2010-09-06
相关资源
最近更新 更多