classRaster_as.data.frame: Convert raster image to a data frame

as.data.frameR Documentation

Convert raster image to a data frame

Description

as.data.frame reorganizes ursaRaster object into data frame, where first two columns (x and y) are coordinates of cells, and the rest columns are cell values.

Usage

## S3 method for class 'ursaRaster'
as.data.frame(x, ...)


# non-public
.as.data.frame(obj, band = FALSE, id = FALSE, na.rm = TRUE, all.na = FALSE,
               col.names = NULL)

Arguments

x, obj

Object of class ursaRaster

...

Set of arguments, which are recognized via their names (using regular expressions) and classes. Passed to non-public .as.data.frame.

Pattern (as.data.frame) Argument (.as.data.frame) Description
band band See below.
id id See below.
na\\.rm na.rm See below.
all\\.na all.na See below.
col(\\.)*name(s)* col.names See below.
band

Logical. If band=FALSE then each band is presented by separate column in the data frame. If band=TRUE then band name is presented as a factor in the column $band, and values are written in the column $z. If band=TRUE then number of rows is

id

Logical. If band=FALSE then is ignored. If id=TRUE then addiditional columns $id will contain unique cell number in the source raster.

na.rm

Logical. If na.rm=FALSE then number of rows for data frame is equal to number of cells of spatial grid of raster. If na.rm=TRUE then cells with 'no data' values for all (all.na=FALSE) or any (all.na=TRUE) bands are omitted.

all.na

Logical. If na.rm=FALSE then ignored. If number of rows for data frame is equal to number of cells of spatial grid of raster. If na.rm=TRUE then cells with 'no data' values for all bands are omitted.

col.names

Character vector or NULL. Names for columns of data frame. If NULL, then column names are generated from band names. Default is NULL.

Details

The structure of voxel is kept. The number of rows for band=TRUE is equal to the number of rows for band=FALSE multiplied to number of bands. To extract all numeric data with destroying of voxel, you may use followed code:
subset(as.data.frame(obj,band=TRUE),!is.na(z)).

Value

Data frame.

If band=TRUE then

x

Horizontal coordinate of cell's midpoint

y

Vertical coordinate of cell's midpoint

z

Value

band

Band as a factor

id

Optional. Unique number for (x,y) coordinate.

If band=FALSE then

x

Horizontal coordinate of cell's midpoint

y

Vertical coordinate of cell's midpoint

...

Additional columns. Names of columns are names of bands. Values of columns are values of corresponded bands.

If ursaRaster is projected, then data frame has additional attribute attr(...,"proj") with value of PROJ.4 string.

Author(s)

Nikita Platonov platonov@sevin.ru

Examples

session_grid(NULL)
session_grid(regrid(res=50000,lim=c(-1200100,-1400800,1600900,1800200)))
a0 <- ursa_dummy(nband=3,min=0,max=100)
a0[a0<30 | a0>70] <- NA
names(a0) <- c("x","y","z")
print(a0)
b0 <- as.data.frame(a0)
session_grid(NULL)
a1 <- as.ursa(b0)
print(a1-a0)
session_grid(NULL)
session_grid(regrid(res=5800000))
set.seed(352)
a2 <- as.integer(ursa_dummy(nband=2,min=0,max=100))
a2[a2>50] <- NA
print(a2)
print(b1 <- as.data.frame(a2,na.rm=FALSE))
print(b2 <- as.data.frame(a2,na.rm=TRUE))
print(b3 <- as.data.frame(a2,all.na=TRUE))
print(b4 <- as.data.frame(a2,band=TRUE,na.rm=FALSE))
print(b5 <- as.data.frame(a2,band=TRUE,all.na=FALSE))
print(b6 <- as.data.frame(a2,band=TRUE,all.na=TRUE))
print(b7 <- as.data.frame(a2,band=TRUE,all.na=TRUE,id=TRUE))

ursa documentation built on Sept. 30, 2024, 9:35 a.m.