我正在使用randomForestrandomForest包中的函数来查找最重要的变量:我的数据框称为城市,我的响应变量是数字收入.
urban.random.forest <- randomForest(revenue ~ .,y=urban$revenue, data = urban, ntree=500, keep.forest=FALSE,importance=TRUE,na.action = na.omit)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Error in randomForest.default(m, y, ...) : data (x) has 0 rows
Run Code Online (Sandbox Code Playgroud)
在源代码上它与x变量有关:
n <- nrow(x)
p <- ncol(x)
if (n == 0)
stop("data (x) has 0 rows")
Run Code Online (Sandbox Code Playgroud)
但我无法理解是什么x.
我解决了这个问题。我有一些列,它们的所有值都是 NA 或相同。我把它们扔掉了,一切顺利。我的列类是字符、数字和因子。
candidatesnodata.index <- c()
for (j in (1 : ncol(dataframe))) {
if ( is.numeric(dataframe[ ,j]) & length(unique(as.numeric(dataframe[ ,j]))) == 1 )
{candidatesnodata.index <- append(candidatesnodata.index,j)}
}
dataframe <- dataframe[ , - candidatesnodata.index]
Run Code Online (Sandbox Code Playgroud)