R编程,对数据进行分类

时间:2018-01-29 18:55:29

标签: r

数据集wineData包含一个名为quality的属性,数值范围从1到8.我试图通过以下代码对其进行分类:

wineData$taste <- NA
wineData$taste[which(wineData$quality< 6)] <- bad
wineData$taste[which(wineData$quality>6)] <- excellent
wineData$taste[which(wineData$quality=6)] <- normal
wineData$taste <- factor(wineData$taste)
wineData`

这里只有质量&gt; 6被归类为优秀,其他所有被归类为NA。为什么它无法对其他人进行分类?

这些是我得到的错误。

  

wineData $ taste [wine(wineData $ quality&gt; 6)]&lt; - 优秀错误:对象&#39;优秀&#39;未找到   wineData $ taste [which(wineData $ quality == 6)]&lt; - normal Error:object&#39; normal&#39;找不到

2 个答案:

答案 0 :(得分:2)

这样做

wineData$taste <- NA
    wineData$taste[which(wineData$quality< 6)] <- "bad"
    wineData$taste[which(wineData$quality>6)] <- "excellent"
    wineData$taste[which(wineData$quality==6)] <- "normal"
    wineData$taste <- factor(wineData$taste)
    wineData

答案 1 :(得分:0)

您也可以使用dplyr::case_when()

library(dplyr)
wineData <- wineData %>%
  mutate(
    taste = case_when(
      quality < 6 ~ "bad",
      quality > 6 ~ "excellent",
      quality = 6 ~ "normal",
      TRUE        ~  NA
    ),
    taste = as.factor(taste, levels = c("bad", "normal", "excellent")
  )