# Cell-level functions¶

## Introduction¶

The cell number is an important concept in the raster package. Raster
data can be thought of as a matrix, but in a `RasterLayer`

it is more
commonly treated as a vector. Cells are numbered from the upper left
cell to the upper right cell and then continuing on the left side of the
next row, and so on until the last cell at the lower-right side of the
raster. There are several helper functions to determine the column or
row number from a cell and vice versa, and to determine the cell number
for x, y coordinates and vice versa.

```
library(raster)
r <- raster(ncol=36, nrow=18)
ncol(r)
## [1] 36
nrow(r)
## [1] 18
ncell(r)
## [1] 648
rowFromCell(r, 100)
## [1] 3
colFromCell(r, 100)
## [1] 28
cellFromRowCol(r,5,5)
## [1] 149
xyFromCell(r, 100)
## x y
## [1,] 95 65
cellFromXY(r, c(0,0))
## [1] 343
colFromX(r, 0)
## [1] 19
rowFromY(r, 0)
## [1] 10
```

## Accessing cell values¶

Cell values can be accessed with several methods. Use `getValues`

to
get all values or a single row; and `getValuesBlock`

to read a block
(rectangle) of cell values.

```
r <- raster(system.file("external/test.grd", package="raster"))
v <- getValues(r, 50)
v[35:39]
## [1] 743.8288 706.2302 646.0078 686.7291 758.0649
getValuesBlock(r, 50, 1, 35, 5)
## [1] 743.8288 706.2302 646.0078 686.7291 758.0649
```

You can also read values using cell numbers or coordinates (xy) using
the `extract`

method.

```
cells <- cellFromRowCol(r, 50, 35:39)
cells
## [1] 3955 3956 3957 3958 3959
extract(r, cells)
## [1] 743.8288 706.2302 646.0078 686.7291 758.0649
xy <- xyFromCell(r, cells)
xy
## x y
## [1,] 179780 332020
## [2,] 179820 332020
## [3,] 179860 332020
## [4,] 179900 332020
## [5,] 179940 332020
extract(r, xy)
## [1] 743.8288 706.2302 646.0078 686.7291 758.0649
```

You can also extract values using SpatialPolygons* or SpatialLines*. The default approach for extracting raster values with polygons is that a polygon has to cover the center of a cell, for the cell to be included. However, you can use argument “weights=TRUE” in which case you get, apart from the cell values, the percentage of each cell that is covered by the polygon, so that you can apply, e.g., a “50% area covered” threshold, or compute an area-weighted average.

In the case of lines, any cell that is crossed by a line is included. For lines and points, a cell that is only ‘touched’ is included when it is below or to the right (or both) of the line segment/point (except for the bottom row and right-most column).

In addition, you can use standard *R* indexing to access values, or to
replace values (assign new values to cells) in a `raster`

object. If
you replace a value in a `raster`

object based on a file, the
connection to that file is lost (because it now is different from that
file). Setting raster values for very large files will be very slow with
this approach as each time a new (temporary) file, with all the values,
is written to disk. If you want to overwrite values in an existing file,
you can use `update`

(with caution!)

```
r[cells]
## [1] 743.8288 706.2302 646.0078 686.7291 758.0649
r[1:4]
## [1] NA NA NA NA
filename(r)
## [1] "C:\\soft\\R\\R-devel\\library\\raster\\external\\test.grd"
r[2:3] <- 10
r[1:4]
## [1] NA 10 10 NA
filename(r)
## [1] ""
```

Note that in the above examples values are retrieved using cell numbers.
That is, a raster is represented as a (one-dimensional) vector. Values
can also be inspected using a (two-dimensional) matrix notation. As for
*R* matrices, the first index represents the row number, the second the
column number.

```
r[1]
## [1] NA
r[2,2]
## [1] NA
r[1,]
## [1] NA 10 10 NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
## [26] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
## [51] NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA NA
## [76] NA NA NA NA NA
r[,2]
## [1] 10.0000 NA NA NA NA NA NA NA
## [9] NA NA NA NA NA NA NA NA
## [17] NA NA NA NA NA NA NA NA
## [25] NA NA NA NA NA NA NA NA
## [33] NA NA NA NA NA NA NA NA
## [41] NA NA NA NA NA NA NA NA
## [49] NA NA NA NA NA NA NA NA
## [57] NA NA NA NA NA NA NA NA
## [65] NA NA NA NA NA NA NA NA
## [73] NA NA NA NA NA NA NA NA
## [81] NA NA NA NA NA NA NA NA
## [89] NA NA NA NA 509.6615 504.0837 495.6735 486.9267
## [97] 479.6082 474.3188 471.4189 469.5895 468.7051 469.8029 472.0836 474.1420
## [105] NA NA NA NA NA NA NA NA
## [113] NA NA NA
r[1:3,1:3]
## [1] NA 10 10 NA NA NA NA NA NA
# keep the matrix structure
r[1:3,1:3, drop=FALSE]
## class : RasterLayer
## dimensions : 3, 3, 9 (nrow, ncol, ncell)
## resolution : 40, 40 (x, y)
## extent : 178400, 178520, 333880, 334000 (xmin, xmax, ymin, ymax)
## crs : +proj=sterea +lat_0=52.1561605555556 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +datum=WGS84 +units=m +no_defs
## source : memory
## names : layer
## values : 10, 10 (min, max)
```

Accessing values through this type of indexing should be avoided inside
functions as it is less efficient than accessing values via functions
like `getValues`

.