R 介绍 <NA>

R introduces <NA>

我正在尝试解决 Data camp Introduction to R 中的问题。问题的陈述如下

As a data analyst, you decide to join Rways, a new airline that only offers flights and services based on data-driven decisions. Business is going well, and the management is considering to extend the offer from only economy class to business and first class as well. You are supposed to process the results of a questionnaire that polls for the preferred class of the respondents. The results for 50 of these respondents can be found in a character vector fly_class.

Convert the fly_class vector to a factor, fly_class_factor. The factor levels should be "economy", "business" and "first". Order the factors if this makes sense in this context.

苍蝇class向量包含

> > fly_class
[1] "eco" "bus" "eco" "bus" "fir" "eco" "eco" "bus" "eco" "eco" "fir" "eco"
[13] "eco" "eco" "eco" "bus" "eco" "eco" "eco" "fir" "bus" "eco" "eco" "fir"
[25] "eco" "bus" "eco" "eco" "eco" "eco" "fir" "fir" "eco" "bus" "eco" "eco"
[37] "bus" "eco" "eco" "eco" "eco" "eco" "eco" "eco" "eco" "eco" "eco" "eco"
[49] "eco" "bus"

当我尝试尝试下面的代码片段时,R 引入了这个因素。我需要知道为什么要引入它。

> fly_class_factor <- factor(fly_class, ordered = TRUE, levels = c("economy", "business", "first"), labels = c("eco", "bus", "fir"))
> fly_class_factor
 [1] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
[16] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
[31] <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA> <NA>
[46] <NA> <NA> <NA> <NA> <NA>
Levels: eco < bus < fir

尝试切换 levelslabels 参数。 levels 应该给出数据的当前值,而 labels 给出您希望它们出现的值。

fly_class <- c("eco", "bus", "eco", "bus", "fir", "eco", "eco", "bus", "eco", "eco", "fir", "eco",
"eco", "eco", "eco", "bus", "eco", "eco", "eco", "fir", "bus", "eco", "eco", "fir",
"eco", "bus", "eco", "eco", "eco", "eco", "fir", "fir", "eco", "bus", "eco", "eco",
"bus", "eco", "eco", "eco", "eco", "eco", "eco", "eco", "eco", "eco", "eco", "eco",
"eco", "bus")

fly_class_factor <- factor(fly_class,
                           levels = c("eco", "bus", "fir"),
                           labels = c("economy", "business", "first"),
                           ordered = TRUE)

fly_class_factor