-2

I import a dataset using the following command:

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)

then I try to do a simple addition:

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

but I am getting this error:

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

How can I fix it?

Here are the two variables needed:

> 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"
Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
adrCoder
  • 3,145
  • 4
  • 31
  • 56
  • 1
    Do not share a link to your dataset. You should provide a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). It should be [minimal, but complete and verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). Your question should be clear and specific. – M-- May 06 '20 at 17:34
  • 1
    `Index` and `D12` are being read as characters, you'll need to convert them to numeric first – dave-edison May 06 '20 at 17:40
  • VTC, not reproducible, the code runs with no errors. – Rui Barradas May 06 '20 at 17:40
  • 1
    The file in the link must have its first line deleted and then read in with `dget`. Then the code runs, no errors. – Rui Barradas May 06 '20 at 17:42
  • You should use `dput` as suggested in the first link I shared with you which gives the structure of your dataset. There's no need to provide all of your dataset; just a portion of it which would reproduce the error. Anyhow, it seems your columns are `character` variables. You need to convert them to `numeric` . Try this: ```annual <- annual[, IndexDiv := as.numeric(Index) + as.numeric(D12)]```. – M-- May 06 '20 at 17:42
  • How do I do that @RuiBarradas? – adrCoder May 06 '20 at 17:42
  • Download the file and then `con <- file(fl, "rt");annual <- dget(con);close(con)` where `fl` is the file name. – Rui Barradas May 06 '20 at 17:49
  • can you please also help me in my other question: https://stackoverflow.com/questions/61637663/r-error-in-dyn-lm-regression-lm-fit-0-non-na-cases I can't get the code to run, I get errors. thanks in advance – adrCoder May 06 '20 at 18:11

1 Answers1

1

You don't need to respecify annual

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

should work