[GIS] Access to attribute in rasterlayer in R

rrasterregressionspatial statistics

I read an ADF file in R:

 kommuner <- raster( "../../../10-Data/Kommunne_i_Norge_2000/Kommuner2000_100m_100m/100x100_2k/w001001.adf" )

It contains three attributes:

> kommuner

class : RasterLayer

dimensions : 14903, 11906, 177435118 (nrow, ncol, ncell)

resolution : 100, 100 (x, y)

extent : -75900, 1114700, 6449500, 7939800 (xmin, xmax, ymin, ymax)

coord. ref. : +proj=utm +zone=33 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0

data source : /path_to/w001001.adf

names : w001001

values : 1, 435 (min, max)

attributes :

   ID COUNT  OBJ

from: 1 63390 0101

to : 435 26048 0903

Now, the question is how I can access the OBJ feature in an easy way. By easy I mean something similar to how I would access a column in a data frame, such as DF$OBJ. Could you help?

Secondly, I would like to create a new attribute that is linked to OBJ. Let's call it NI. How can I create the NI attribute, say, let NI = 4 + OBJ * 5?

Lastly, I would like to run a regression with the NI feature in this kommuner variable, which is the y-variable, against a number of other features in other variables. Could you provide an example so that I can imitate?

Best Answer

You can access this table via levels:

> levels(r)
[[1]]
   ID OID Value    Count LC_Code         Land_Cover    Area_Ha
1   0   0     0 25615512     255            No Data 2305400.00
2   1   1     1  1287546       1       Dense Forest  115879.00
3   2   2     2   525368       2    Moderate Forest   47283.10
4   3   3     3  2993504       3      Sparse Forest  269415.00

Read ?raster::levels for more.

If you want to modify it, fit models with it, etc, you can possibly modify it in-place, but it might be easier to just get it out, play with it, and put it back when you're done:

> z = levels(r)[[1]]

Create a new variable in z:

> z$r = z$Area_Ha/z$Count
> head(z)
  ID OID Value    Count LC_Code       Land_Cover    Area_Ha          r
1  0   0     0 25615512     255          No Data 2305400.00 0.09000015
2  1   1     1  1287546       1     Dense Forest  115879.00 0.08999989
3  2   2     2   525368       2  Moderate Forest   47283.10 0.08999996
4  3   3     3  2993504       3    Sparse Forest  269415.00 0.08999988
5  4   4     4        3      20         Woodland       0.27 0.09000000
6  5   5     5   349569      31 Closed Grassland   31461.20 0.08999997

Stick it back in:

> levels(r)[[1]]=z
> levels(r)
[[1]]
   ID OID Value    Count LC_Code         Land_Cover    Area_Ha          r
1   0   0     0 25615512     255            No Data 2305400.00 0.09000015
2   1   1     1  1287546       1       Dense Forest  115879.00 0.08999989
3   2   2     2   525368       2    Moderate Forest   47283.10 0.08999996
4   3   3     3  2993504       3      Sparse Forest  269415.00 0.08999988

Notice its a list of length one, because (I think) I have a single-layer raster. It might be a longer list if you have a RasterStack.

Related Question