I have a raster (seg) whose values I want to substitute, and am getting an error from function subs from the raster package that I don't understand at all.
I get the following error
Error in .local(x, y, ...) : duplicate "by" values not allowed
Even though I have no duplicates in my data frame.
The error show up with both of the following commands, which makes no sense to me.
seg <- subs(seg, tabulated, by="seg", which="class")
seg <- subs(seg, tabulated[which(!duplicated(tabulated$seg)),], by="seg", which="class")
From what i could find out from subs, it checks duplicates through the following commands
tt <- table(tabulated$seg)
tt <- tt[ which(tt > 1) ]
if (length(tt) > 0) {
stop('duplicate "by" values not allowed')
}
When I run those commands myself I find that tt has a length of 0...
I am not sure how to share the specifics, so I copied the structure of the two objects. The dataframe has 5784018 rows, which corresponds to the number of unique values of the raster seg.
I am not sure what I am missing.
The structure of my raster is the following
Formal class 'RasterLayer' [package "raster"] with 12 slots
..@ file :Formal class '.RasterFile' [package "raster"] with 13 slots
.. .. ..@ name : chr "/media/olivier/olivier_ext/gedata_current/jde_coffee/data/ES/Classifs/test/es3_seg2.tif"
.. .. ..@ datanotation: chr "INT4S"
.. .. ..@ byteorder : chr "little"
.. .. ..@ nodatavalue : num -Inf
.. .. ..@ NAchanged : logi FALSE
.. .. ..@ nbands : int 1
.. .. ..@ bandorder : chr "BIL"
.. .. ..@ offset : int 0
.. .. ..@ toptobottom : logi TRUE
.. .. ..@ blockrows : int 1
.. .. ..@ blockcols : int 11497
.. .. ..@ driver : chr "gdal"
.. .. ..@ open : logi FALSE
..@ data :Formal class '.SingleLayerData' [package "raster"] with 13 slots
.. .. ..@ values : logi(0)
.. .. ..@ offset : num 0
.. .. ..@ gain : num 1
.. .. ..@ inmemory : logi FALSE
.. .. ..@ fromdisk : logi TRUE
.. .. ..@ isfactor : logi FALSE
.. .. ..@ attributes: list()
.. .. ..@ haveminmax: logi TRUE
.. .. ..@ min : num -2.15e+09
.. .. ..@ max : num 2.15e+09
.. .. ..@ band : int 1
.. .. ..@ unit : chr ""
.. .. ..@ names : chr "es3_seg2"
..@ legend :Formal class '.RasterLegend' [package "raster"] with 5 slots
.. .. ..@ type : chr(0)
.. .. ..@ values : logi(0)
.. .. ..@ color : logi(0)
.. .. ..@ names : logi(0)
.. .. ..@ colortable: logi(0)
..@ title : chr(0)
..@ extent :Formal class 'Extent' [package "raster"] with 4 slots
.. .. ..@ xmin: num 268734
.. .. ..@ xmax: num 337716
.. .. ..@ ymin: num 7714422
.. .. ..@ ymax: num 7828590
..@ rotated : logi FALSE
..@ rotation:Formal class '.Rotation' [package "raster"] with 2 slots
.. .. ..@ geotrans: num(0)
.. .. ..@ transfun:function ()
..@ ncols : int 11497
..@ nrows : int 19028
..@ crs :Formal class 'CRS' [package "sp"] with 1 slot
.. .. ..@ projargs: chr "+proj=utm +zone=24 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0"
..@ history : list()
..@ z : list()
And the structure of the data frame is
'data.frame': 5784018 obs. of 3 variables:
$ class: int 2 6 6 2 2 6 11 7 7 7 ...
$ seg : int 1 3 6 8 10 15 18 21 22 24 ...
$ freq : int 2504 375 31 4 102 109 3 6 7 7 ...