R: Convert Data To Class psp

as.psp {spatstat.geom} | R Documentation

## Convert Data To Class psp

### Description

Tries to coerce any reasonable kind of data object to a line segment pattern (an object of class `"psp"`) for use by the spatstat package.

### Usage

      as.psp(x, ..., from=NULL, to=NULL)

      ## S3 method for class 'psp'
    as.psp(x, ..., check=FALSE, fatal=TRUE)

      ## S3 method for class 'data.frame'
    as.psp(x, ..., window=NULL, marks=NULL,
          check=spatstat.options("checksegments"), fatal=TRUE)

      ## S3 method for class 'matrix'
    as.psp(x, ..., window=NULL, marks=NULL,
           check=spatstat.options("checksegments"), fatal=TRUE)

      ## Default S3 method:
    as.psp(x, ..., window=NULL, marks=NULL,
           check=spatstat.options("checksegments"), fatal=TRUE)

### Arguments

`x` | Data which will be converted into a line segment pattern
`window` | Data which define a window for the pattern.
`...` | Ignored.
`marks` | (Optional) vector or data frame of marks for the pattern
`check` | Logical value indicating whether to check the validity of the data, e.g. to check that the line segments lie inside the window.
`fatal` | Logical value. See Details.
`from,to` | Point patterns (object of class `"ppp"`) containing the first and second endpoints (respectively) of each segment. Incompatible with `x`.

### Details

Converts the dataset `x` to a line segment pattern (an object of class `"psp"`; see `psp.object` for an overview).

This function is normally used to convert an existing line segment pattern dataset, stored in another format, to the `"psp"` format. To create a new point pattern from raw data such as x,y coordinates, it is normally easier to use the creator function `psp`.

The dataset `x` may be:

- an object of class `"psp"`

- a data frame with at least 4 columns

- a structure (list) with elements named `x0, y0, x1, y1` or elements named `xmid, ymid, length, angle` and possibly a fifth element named `marks`

If `x` is a data frame the interpretation of its columns is as follows:

- If there are columns named `x0, y0, x1, y1` then these will be interpreted as the coordinates of the endpoints of the segments and used to form the `ends` component of the `psp` object to be returned.

- If there are columns named `xmid, ymid, length, angle` then these will be interpreted as the coordinates of the segment midpoints, the lengths of the segments, and the orientations of the segments in radians and used to form the `ends` component of the `psp` object to be returned.

- If there is a column named `marks` then this will be interpreted as the marks of the pattern provided that the argument `marks` of this function is `NULL`. If argument `marks` is not `NULL` then the value of this argument is taken to be the marks of the pattern and the column named `marks` is ignored (with a warning). In either case the column named marks is deleted and omitted from further consideration.

- If there is no column named `marks` and if the `marks` argument of this function is `NULL`, and if after interpreting 4 columns of `x` as determining the `ends` component of the `psp` object to be returned, there remain other columns of `x`, then these remaining columns will be taken to form a data frame of marks for the `psp` object to be returned.

If `x` is a structure (list) with elements named `x0, y0, x1, y1, marks` or `xmid, ymid, length, angle, marks`, then the element named `marks` will be interpreted as the marks of the pattern provide that the argument `marks` of this function is `NULL`. If this argument is non-`NULL` then it is interpreted as the marks of the pattern and the element `marks` of `x` is ignored — with a warning.

Alternatively, you may specify two point patterns `from` and `to` containing the first and second endpoints of the line segments.

The argument `window` is converted to a window object by the function `as.owin`.

The argument `fatal` indicates what to do when the data cannot be converted to a line segment pattern. If `fatal=TRUE`, a fatal error will be generated; if `fatal=FALSE`, the value `NULL` is returned.

The function `as.psp` is generic, with methods for the classes `"psp"`, `"data.frame"`, `"matrix"` and a default method.

Point pattern datasets can also be created by the function `psp`.

### Value

An object of class `"psp"` (see `psp.object`) describing the line segment pattern and its window of observation. The value `NULL` may also be returned; see Details.

### Warnings

If only a proper subset of the names `x0,y0,x1,y1` or `xmid,ymid,length,angle` appear amongst the names of the columns of `x` where `x` is a data frame, then these special names are ignored.

For example if the names of the columns were `xmid,ymid,length,degrees`, then these columns would be interpreted as if the represented `x0,y0,x1,y1` in that order.

Whether it gets used or not, column named `marks` is always removed from `x` before any attempt to form the `ends` component of the `psp` object that is returned.

### Author(s)

Adrian Baddeley Adrian.Baddeley@curtin.edu.au, Rolf Turner r.turner@auckland.ac.nz and Ege Rubak rubak@math.aau.dk.

### See Also

`psp`, `psp.object`, `as.owin`, `owin.object`.

See `edges` for extracting the edges of a polygonal window as a `"psp"` object.

### Examples

       mat <- matrix(runif(40), ncol=4)
       mx <- data.frame(v1=sample(1:4,10,TRUE),
                        v2=factor(sample(letters[1:4],10,TRUE),levels=letters[1:4]))
       a <- as.psp(mat, window=owin(),marks=mx)
       mat <- cbind(as.data.frame(mat),mx)
       b <- as.psp(mat, window=owin()) # a and b are identical.
       stuff <- list(xmid=runif(10),
                     ymid=runif(10),
                     length=rep(0.1, 10),
                     angle=runif(10, 0, 2 * pi))
       a <- as.psp(stuff, window=owin())
       b <- as.psp(from=runifrect(10), to=runifrect(10))

[Package spatstat.geom version 2.3-2 Index]