降低因子变量的水平
Reduce levels from factor variable
我必须在大型火车集中做一个随机森林,但我不能使用超过 53 个级别的变量。
我需要减少的因子变量 (train$tip
) 有 150 个级别(KHC、KTF、KGL 等)。我如何(快速)删除(或仅保留 53 个级别)出现次数较少的级别并保留数量更多的级别?
我是不是要把我看到的所有关卡名字都写出来,或者有没有更快的方法?
train <- train[!train$tip == "KTF", ]
你可以这样做:
train <- train[train$tip %in% names(sort(table(train$tip), decreasing = TRUE))[1:53], ]
table()
计算水平的频率; sort()
降序排列; names()
获取电平而不是频率; [
只选择前 53 个。
我必须在大型火车集中做一个随机森林,但我不能使用超过 53 个级别的变量。
我需要减少的因子变量 (train$tip
) 有 150 个级别(KHC、KTF、KGL 等)。我如何(快速)删除(或仅保留 53 个级别)出现次数较少的级别并保留数量更多的级别?
我是不是要把我看到的所有关卡名字都写出来,或者有没有更快的方法?
train <- train[!train$tip == "KTF", ]
你可以这样做:
train <- train[train$tip %in% names(sort(table(train$tip), decreasing = TRUE))[1:53], ]
table()
计算水平的频率; sort()
降序排列; names()
获取电平而不是频率; [
只选择前 53 个。