R-二进制运算符的非数字参数

时间:2020-05-06 17:31:46

标签: r data.table

我使用以下命令导入数据集:

monthly <- read.csv2("C:/Users/adr/Desktop/dataset/Replication/monthly_2005.csv", 
                     na.strings="NaN", stringsAsFactors=FALSE)
annual  <- read.csv2("C:/Users/adr/Desktop/dataset/Replication/annual_2005.csv", 
                     na.strings="NaN", stringsAsFactors=FALSE)
annual  <- as.data.table(annual)

然后我尝试做一个简单的加法:

annual <- annual[, IndexDiv := Index + D12]

但我收到此错误:

Error in Index + D12 : non-numeric argument to binary operator

我该如何解决?

这是需要的两个变量:

> annual[,Index]
  [1] "4.74"    "5.07"    "4.42"    "4.54"    "4.37"    "3.58"    "3.25"    "3.45"    "4.92"    "5.84"    "6.01"   
 [12] "5.84"    "5.34"    "4.34"    "5.20"    "5.64"    "5.27"    "5.14"    "5.32"    "4.60"    "5.41"    "5.51"   
 [23] "4.41"    "4.30"    "4.32"    "4.22"    "4.75"    "5.65"    "6.02"    "6.87"    "7.95"    "8.05"    "6.57"   
 [34] "8.25"    "9.54"    "9.84"    "6.57"    "9.03"    "10.30"   "9.05"    "9.11"    "9.38"    "8.04"    "7.35"   
 [45] "9.48"    "9.80"    "6.80"    "7.90"    "8.92"    "6.81"    "7.31"    "8.78"    "8.55"    "10.16"   "12.46"  
 [56] "13.49"   "17.66"   "24.35"   "21.45"   "15.34"   "8.12"    "6.89"    "10.10"   "9.50"    "13.43"   "17.18"  
 [67] "10.55"   "13.21"   "12.49"   "10.58"   "8.69"    "9.77"    "11.67"   "13.28"   "17.36"   "15.30"   "15.30"  
 [78] "15.20"   "16.76"   "20.41"   "23.77"   "26.57"   "24.81"   "35.98"   "45.48"   "46.67"   "39.99"   "55.21"  
 [89] "59.89"   "58.11"   "71.55"   "63.10"   "75.02"   "84.75"   "92.43"   "80.33"   "96.47"   "103.86"  "92.06"  
[100] "92.15"   "102.09"  "118.05"  "97.55"   "68.56"   "90.19"   "107.46"  "95.10"   "96.11"   "107.94"  "135.76" 
[111] "122.55"  "140.64"  "164.93"  "167.24"  "211.28"  "242.17"  "247.08"  "277.72"  "353.40"  "330.22"  "417.09" 
[122] "435.71"  "466.45"  "459.27"  "615.93"  "740.74"  "970.43"  "1229.23" "1469.25" "1320.28" "1148.08" "879.82" 
[133] "1111.92" "1211.92" "1248.29"
> annual[,D12]
  [1] "0.260"  "0.300"  "0.330"  "0.330"  "0.300"  "0.300"  "0.190"  "0.180"  "0.200"  "0.260"  "0.320"  "0.320" 
 [13] "0.330"  "0.310"  "0.240"  "0.220"  "0.250"  "0.230"  "0.220"  "0.220"  "0.220"  "0.240"  "0.250"  "0.210" 
 [25] "0.190"  "0.180"  "0.180"  "0.200"  "0.210"  "0.300"  "0.320"  "0.330"  "0.350"  "0.310"  "0.330"  "0.400" 
 [37] "0.440"  "0.400"  "0.440"  "0.470"  "0.470"  "0.480"  "0.480"  "0.420"  "0.430"  "0.560"  "0.690"  "0.570" 
 [49] "0.530"  "0.510"  "0.460"  "0.510"  "0.530"  "0.550"  "0.600"  "0.690"  "0.770"  "0.850"  "0.970"  "0.980" 
 [61] "0.820"  "0.500"  "0.440"  "0.450"  "0.470"  "0.720"  "0.800"  "0.510"  "0.620"  "0.670"  "0.710"  "0.590" 
 [73] "0.610"  "0.640"  "0.660"  "0.710"  "0.840"  "0.930"  "1.140"  "1.470"  "1.410"  "1.410"  "1.450"  "1.540" 
 [85] "1.640"  "1.740"  "1.790"  "1.750"  "1.830"  "1.950"  "2.020"  "2.130"  "2.280"  "2.500"  "2.720"  "2.870" 
 [97] "2.920"  "3.070"  "3.160"  "3.120"  "3.070"  "3.160"  "3.400"  "3.620"  "3.690"  "4.090"  "4.710"  "5.120" 
[109] "5.700"  "6.240"  "6.650"  "6.890"  "7.120"  "7.570"  "7.940"  "8.300"  "8.505"  "9.730"  "11.050" "12.090"
[121] "12.200" "12.380" "12.580" "13.180" "13.790" "14.900" "15.490" "16.200" "16.690" "16.270" "15.740" "16.077"
[133] "17.385" "19.440" "22.220"

1 个答案:

答案 0 :(得分:1)

您无需重新指定annual

annual[, IndexDiv := as.numeric(Index) + as.numeric(D12)]

应该工作