簡體   English   中英

為什么會出現“因子級別[2]重復”的錯誤?

[英]Why the error “factor level [2] is duplicated” will occur?

我嘗試使用因子(月)將月份變量(整數)轉換為分類變量,但由於錯誤而失敗。 我該如何解決?

這是我的代碼:

library(tidyverse)
library(dplyr)
install.packages("nycflights13")
library(nycflights13)
month_new <- flights$month
month_new
flights %>%
   filter(dest == "HNL", air_time > 10) %>%
   factor(month_new) %>%
   ggplot(x = month_new) + geom_bar()

您的分配factor(month_new)不起作用。 我建議mutate(month = as.factor(month))並且沒有美學aes

library(tidyverse)
#install.packages("nycflights13")
library(nycflights13)

  flights %>%
    filter(dest == "HNL", air_time > 10) %>%
    mutate(month = as.factor(month)) %>%
    ggplot(aes(x = month)) + 
    geom_bar()

或者:

library(tidyverse)
#install.packages("nycflights13")
library(nycflights13)

flights %>%
  filter(dest == "HNL", air_time > 10) 

  ggplot(flights, aes(x=factor(month)))+
    geom_bar(fill="steelblue")+
    theme_minimal()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM