数据框'vpsots'有一个变量'type',有13个级别(如下所示),而在探索数据框时,它试图用suv重新整合SUV(只是为了看看我能不能)。我应该在下面的代码中对警告做些什么。我看到SUV改为NA了。我认为它与变量'type'是一个因素有关。是因为级别'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
答案 0 :(得分:1)
要重命名因子的级别,您可以使用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