r - if(any(co)){: missing value where TRUE/FALSE needed In addition: Warning messages: 1: In FUN(newX[, i], …) : NAs introduced by coercion

标签 r dataframe error-handling na kernlab

我正在使用大约150000行和25列的数据集。数据由数值和因子变量组成。因素变量既是文本又是数字,我需要所有这些。因变量是一个具有20个水平的因子。
我正在尝试使用R中的kernlab包构建模型并将其馈送到SVM。

library(kernlab)
n<- nrow(x)
trainInd<- sort(sample(1:nrow(x), n*.8))
xtrain<- x[trainInd,]
xtest<- x[-trainInd,]
ytrain<- y[trainInd]
ytest<- y[-trainInd]
modelclass<- ksvm(x=as.matrix(xtrain), y=as.matrix(ytrain),
              scaled = TRUE, type="C-svc", kernel = "rbfdot",
              kpar="automatic", C=1, cross=0) 

按照代码,我得到此错误:
Error in if (any(co)) { : missing value where TRUE/FALSE needed
In addition: Warning messages:
1: In FUN(newX[, i], ...) : NAs introduced by coercion
xtrain数据框如下所示:
Length    Gender    Age    Day    Hour     Duration    Period
  5         1       80      5      11         20          3
 0.2        2       35      2      18         10          5    
 1.1        2       55      1      15         120         4

“性别”,“日期”和“期间”变量是类别(因子),其余为数字。

我曾经经历过类似的问题,也曾经经历过我的数据集,但是我无法确定任何NA值或其他错误。

我假设我在变量类型,尤其是因素方面做错了。我不确定如何使用它们,但是看不到任何错误。
任何有关如何解决误差的帮助,以及可能如何与数字变量一起建模的因素,将不胜感激。

最佳答案

我在R中使用e1071包遇到了同样的问题。我解决了将所有变量更改为numeric而不是factor的问题,但决策变量(y)除外,决策变量可以是一个因素(用于分类任务)或一个数字(用于回归)。

引用文献:

CRAN Package 'e1071'

关于r - if(any(co)){: missing value where TRUE/FALSE needed In addition: Warning messages: 1: In FUN(newX[, i], …) : NAs introduced by coercion,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44875406/

相关文章:

c# - 是否可以在没有用户登录的情况下更改异常(exception)用户?

r - 这个空格像千位分隔符是什么特殊字符?

python - 将非标准化的键/值列合并为标准化形式

r - 从列表元素的成对组合构建数据框

python - Pandas 数据框两列的交集

angularjs - 如何在显示输入字段错误消息时使用angularjs表单方法

r - 如何调整数据表的大小以将其放入 Shiny 仪表板的框()中

r - 基于组的图

r - 在默认浏览器中直接打开 Shiny App

angularjs - ASP.NET Core Web Api 向 AngularJS $http promise 返回错误消息