数据帧'vpsots'有一个变量'类型',有13个级别(如下所示),在探索数据帧的同时,它试图保持SUV与SUV(只是看看我能不能)。我应该如何理解下面代码中的警告呢?我看到那辆SUV被换成了NA。我认为这与变量"类型"是一个因素有关。是因为"suv"级别不存在吗?我正在努力提高阅读警告标志的能力,并希望得到建议。
> unique(vposts$type)
[1] coupe SUV sedan hatchback wagon van <NA>
[8] convertible pickup truck mini-van other bus offroad
13 Levels: bus convertible coupe hatchback mini-van offroad other pickup sedan SUV ... wagon
> vposts$type[vposts$type=="SUV"]="suv"
Warning message:
In `[<-.factor`(`*tmp*`, vposts$type == "SUV", value = c(3L, NA, :
invalid factor level, NA generated
> unique(vposts$type)
[1] coupe <NA> sedan hatchback wagon van convertible
[8] pickup truck mini-van other bus offroad
13 Levels: bus convertible coupe hatchback mini-van offroad other pickup sedan SUV ... wagon
为了重命名因子的级别,您可以使用levels
函数执行以下操作:
# Create a factor with each alphabet letter as levels.
a_factor <- factor(substring("statistics", 1:10, 1:10), levels = letters)
summary(a_factor)
a b c d e f g h i j k l m n o p q r s t u v w x y z
1 0 1 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 3 3 0 0 0 0 0 0
# Rename the level whose name is "c".
levels(a_factor)[levels(a_factor) == "c"] <- "CE"
summary(a_factor)
a b CE d e f g h i j k l m n o p q r s t u v w x y z
1 0 1 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 3 3 0 0 0 0 0 0