从命令行运行R文件时出错

从命令行运行R文件时出错,r,command-line,R,Command Line,我有一个R文件,它导入一个文件,进行一些数据操作,并执行逻辑回归模型,然后将这些结果保存到一个txt文件中。但是,当我从命令行运行该文件时,我收到以下错误消息,不知道发生了什么 anonymous@anonymous-Latitude-E6520:~/Downloads$ R --no-save < Auto_Model.r > out.txt Warning message: NAs introduced by coercion Error in if (x == "\\N")

我有一个R文件,它导入一个文件,进行一些数据操作,并执行逻辑回归模型,然后将这些结果保存到一个txt文件中。但是,当我从命令行运行该文件时,我收到以下错误消息,不知道发生了什么

anonymous@anonymous-Latitude-E6520:~/Downloads$ R --no-save < Auto_Model.r > out.txt
Warning message:
NAs introduced by coercion 
Error in if (x == "\\N") NA else if (x > 1 & x < 6999) "1:6999" else if (x >  : 
  missing value where TRUE/FALSE needed
Calls: bin.value -> do.call -> mapply -> .Call -> <Anonymous>
Execution halted
anonymous@anonymous-Latitude-E6520:~/Downloads$ R --no-save < Auto_Model.r

在我看来,您尝试执行的操作已经由函数
cut
完成:

bin.value <- function(x){
    cut(as.integer(x),
        breaks= c(1,6999,9999,14849,19699,29999,31999,34999,42999,49999,59999,69999,79999,89999,96999,99820),
        labels= c("1:6999", "7000:9999", "10000:14849", "14850:19699", "19700:29999", "30000:31999", "32000:34999", "35000:42999", "43000:49999", "50000:59999", "60000:69999", "70000:79999", "80000:89999", "90000:96999", "97000:99820"))
    }
因此,
\\N
被直接列为
NA
,循环只在末尾处理,同时所有
if
语句尝试将缺少的值与某些元素进行比较

as.integer(a)[1]=="\\N"
[1] NA # Instead of TRUE or FALSE

谢谢,这是一种更优雅的存储变量的方法。
bin.value <- function(x){
    cut(as.integer(x),
        breaks= c(1,6999,9999,14849,19699,29999,31999,34999,42999,49999,59999,69999,79999,89999,96999,99820),
        labels= c("1:6999", "7000:9999", "10000:14849", "14850:19699", "19700:29999", "30000:31999", "32000:34999", "35000:42999", "43000:49999", "50000:59999", "60000:69999", "70000:79999", "80000:89999", "90000:96999", "97000:99820"))
    }
a <- c("\\N",sample(seq(0,100000,by=1),10))
a
[1] "\\N"   "38987" "50403" "75683" "66706" "27924" "17216" "77539" "80658" "2335"  "53010"
as.integer(a)
[1]    NA 38987 50403 75683 66706 27924 17216 77539 80658  2335 53010
as.integer(a)[1]=="\\N"
[1] NA # Instead of TRUE or FALSE