non meaningful operation for fractor error when storing new value in data frame: R -
i trying update a value in data frame getting--what seems me--a weird error operation don't think using.
here's summary of data:
> str(us.cty2015@data) 'data.frame': 3108 obs. of 15 variables: $ statefp : factor w/ 52 levels "01","02","04",..: 17 25 33 46 4 14 16 24 36 42 ... $ countyfp : factor w/ 325 levels "001","003","005",..: 112 91 67 9 43 81 7 103 72 49 ... $ countyns : factor w/ 3220 levels "00023901","00025441",..: 867 1253 1600 2465 38 577 690 1179 1821 2104 ... $ affgeoid : factor w/ 3220 levels "0500000us01001",..: 976 1472 1879 2813 144 657 795 1395 2098 2398 ... $ geoid : factor w/ 3220 levels "01001","01003",..: 976 1472 1879 2813 144 657 795 1395 2098 2398 ... $ name : factor w/ 1910 levels "abbeville","acadia",..: 1558 1703 1621 688 856 1075 148 1807 1132 868 ... $ lsad : factor w/ 9 levels "00","03","04",..: 5 5 5 5 5 5 5 5 5 5 ... $ aland : num 1.66e+09 1.10e+09 3.60e+09 2.12e+08 1.50e+09 ... $ awater : num 2.78e+06 5.24e+07 3.50e+07 2.92e+08 8.91e+06 ... $ t_pop : num 0 0 0 0 0 0 0 0 0 0 ... $ n_wht : num 0 0 0 0 0 0 0 0 0 0 ... $ n_free_blk: num 0 0 0 0 0 0 0 0 0 0 ... $ n_slv : num 0 0 0 0 0 0 0 0 0 0 ... $ n_blk : num 0 0 0 0 0 0 0 0 0 0 ... $ n_free : num 0 0 0 0 0 0 0 0 0 0 ... > str(us.cty1860@data) 'data.frame': 2126 obs. of 29 variables: $ decade : factor w/ 1 level "1860": 1 1 1 1 1 1 1 1 1 1 ... $ nhgisnam : factor w/ 1236 levels "abbeville","accomack",..: 1142 1218 1130 441 812 548 1144 56 50 887 ... $ nhgisst : factor w/ 41 levels "010","050","060",..: 32 13 9 36 16 36 16 30 23 39 ... $ nhgiscty : factor w/ 320 levels "0000","0010",..: 142 206 251 187 85 231 131 12 6 161 ... $ icpsrst : factor w/ 37 levels "1","11","12",..: 5 13 21 26 22 26 22 10 15 17 ... $ icpsrcty : factor w/ 273 levels "10","1010","1015",..: 25 93 146 72 247 122 12 10 228 45 ... $ icpsrnam : factor w/ 1200 levels "abbeville","accomack",..: 1108 1184 1097 432 791 535 1110 55 49 860 ... $ statenam : factor w/ 41 levels "alabama","arkansas",..: 32 13 9 36 16 36 16 30 23 39 ... $ icpsrsti : int 14 31 44 49 45 49 45 24 34 40 ... $ icpsrctyi : int 1210 1970 2910 1810 710 2450 1130 110 50 1450 ... $ icpsrfip : num 0 0 0 0 0 0 0 0 0 0 ... $ state : factor w/ 41 levels "010","050","060",..: 32 13 9 36 16 36 16 30 23 39 ... $ county : factor w/ 320 levels "0000","0010",..: 142 206 251 187 85 231 131 12 6 161 ... $ pid : num 1538 735 306 1698 335 ... $ x_centroid : num 1348469 184343 1086494 -62424 585888 ... $ y_centroid : num 556680 588278 -229809 -433290 -816852 ... $ gisjoin : factor w/ 2126 levels "g0100010","g0100030",..: 1585 627 319 1769 805 1788 823 1425 1079 2006 ... $ gisjoin2 : factor w/ 2126 levels "0100010","0100030",..: 1585 627 319 1769 805 1788 823 1425 1079 2006 ... $ shape_area : num 2.35e+09 1.51e+09 8.52e+08 2.54e+09 6.26e+08 ... $ shape_len : num 235777 155261 166065 242608 260615 ... $ t_pop : int 25043 653 4413 8184 174491 1995 4324 17187 4649 8392 ... $ n_wht : int 24974 653 4295 6892 149063 1684 3001 17123 4578 2580 ... $ n_free_blk : int 69 0 2 0 10939 2 7 64 12 409 ... $ n_slv : int 0 0 116 1292 14484 309 1316 0 59 5403 ... $ n_blk : int 69 0 118 1292 25423 311 1323 64 71 5812 ... $ n_free : num 25043 653 4297 6892 160007 ... $ frac_free : num 1 1 0.974 0.842 0.917 ... $ frac_free_blk: num 1 na 0.0169 0 0.4303 ... $ frac_slv : num 0 0 0.0263 0.1579 0.083 ... > str(overlap) 'data.frame': 15266 obs. of 7 variables: $ cty2015 : factor w/ 3108 levels "0","1","10","100",..: 1 1 2 2 2 2 2 1082 1082 1082 ... $ cty1860 : factor w/ 2126 levels "0","1","10","100",..: 1047 1012 1296 1963 2033 2058 2065 736 1413 1569 ... $ area_inter : num 1.66e+09 2.32e+05 9.81e+04 1.07e+09 7.67e+07 ... $ area1860 : num 1.64e+11 1.81e+11 1.54e+09 2.91e+09 2.32e+09 ... $ frac_1860 : num 1.01e-02 1.28e-06 6.35e-05 3.67e-01 3.30e-02 ... $ sum_frac_1860 : num 1 1 1 1 1 ... $ scaled_frac_1860: num 1.01e-02 1.28e-06 6.35e-05 3.67e-01 3.30e-02 ...
i trying multiply vector of variables vars <- c("t_pop", "n_wht", "n_free_blk", "n_slv", "n_blk", "n_free")
in us.cty1860@data
data frame scalar overlap$scaled_frac_1860[i]
, add same vector of variables in us.cty2015@data
data frame, , overwrite variables in us.cty2015@data
data frame.
when make following call, error seems saying trying preform invalid operations on factors (which not case (you can confirm str
output)).
> us.cty2015@data[overlap$cty2015[1], vars] <- us.cty2015@data[overlap$cty2015[1], vars] + (overlap$scaled_frac_1860[1] * us.cty1860@data[overlap$cty1860[1], vars]) error in summary.factor(1l, na.rm = false) : ‘max’ not meaningful factors in addition: warning message: in ops.factor(i, 0l) : ‘>=’ not meaningful factors
however, when don't attempt overwrite old value, operation works fine.
> us.cty2015@data[overlap$cty2015[1], vars] + (overlap$scaled_frac_1860[1] * us.cty1860@data[overlap$cty1860[1], vars]) t_pop n_wht n_free_blk n_slv n_blk n_free 0 118.3889 113.6468 0.1317233 4.610316 4.742039 113.7785
i'm sure there better ways of accomplishing trying have idea going on?
edit:
i using following libraries: rgdal
, rgeos
, , maptools
the data/object coming nhgis shapefiles 1860 , 2015 united states counties.
Comments
Post a Comment