我有一个 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
导致错误的R脚本如下=
> ## IMPORT DATA:
> #setwd("~/Desktop")
> library(foreign)
> dat = read.csv("dat.csv", stringsAsFactors=FALSE)
>
> ## zipcode =
> dat$zipcode = as.character(dat$zipcode)
>
> bin.value = Vectorize(function(x) {
+ if (x == "\\N") NA
+ else if (x > 1 & x < 6999) "1:6999"
+ else if (x > 7000 & x < 9999) "7000:9999"
+ else if (x > 10000 & x < 14849) "10000:14849"
+ else if (x > 14850 & x < 19699) "14850:19699"
+ else if (x > 19700 & x < 29999) "19700:29999"
+ else if (x > 30000 & x < 31999) "30000:31999"
+ else if (x > 32000 & x < 34999) "32000:34999"
+ else if (x > 35000 & x < 42999) "35000:42999"
+ else if (x > 43000 & x < 49999) "43000:49999"
+ else if (x > 50000 & x < 59999) "50000:59999"
+ else if (x > 60000 & x < 69999) "60000:69999"
+ else if (x > 70000 & x < 79999) "70000:79999"
+ else if (x > 80000 & x < 89999) "80000:89999"
+ else if (x > 90000 & x < 96999) "90000:96999"
+ else if (x > 97000 & x < 99820) "97000:99820"
+ else NA
+ })
>
> dat$zipcode2 = as.character(bin.value(as.integer(dat$zipcode)))
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
我认为我尝试操纵 zipcode 变量模式的方式有些错误,但我尝试过的任何方法似乎都无法解决问题。
> str(dat$zipcode)
int [1:12635] 76148 33825 61832 11368 98290 92078 44104 62052 55106 20861 ...
>