# Extract.ppp

##### Extract or Replace Subset of Point Pattern

Extract or replace a subset of a point pattern. Extraction of a subset has the effect of thinning the points and/or trimming the window.

##### Usage

```
# S3 method for ppp
[(x, i, j, drop=FALSE, …, clip=FALSE)
# S3 method for ppp
[(x, i, j) <- value
```

##### Arguments

- x
A two-dimensional point pattern. An object of class

`"ppp"`

.- i
Subset index. Either a valid subset index in the usual R sense, indicating which points should be retained, or a window (an object of class

`"owin"`

) delineating a subset of the original observation window, or a pixel image with logical values defining a subset of the original observation window.- value
Replacement value for the subset. A point pattern.

- j
Redundant. Included for backward compatibility.

- drop
Logical value indicating whether to remove unused levels of the marks, if the marks are a factor.

- clip
Logical value indicating how to form the window of the resulting point pattern, when

`i`

is a window. If`clip=FALSE`

(the default), the result has window equal to`i`

. If`clip=TRUE`

, the resulting window is the intersection between the window of`x`

and the window`i`

.- …
Ignored. This argument is required for compatibility with the generic function.

##### Details

These functions extract a designated subset of a point pattern, or replace the designated subset with another point pattern.

The function `[.ppp`

is a method for `[`

for the
class `"ppp"`

. It extracts a designated subset of a point pattern,
either by ``*thinning*''
(retaining/deleting some points of a point pattern)
or ``*trimming*'' (reducing the window of observation
to a smaller subregion and retaining only
those points which lie in the subregion) or both.

The pattern will be ``thinned''
if `i`

is a subset index in the usual R sense:
either a numeric vector
of positive indices (identifying the points to be retained),
a numeric vector of negative indices (identifying the points
to be deleted) or a logical vector of length equal to the number of
points in the point pattern `x`

. In the latter case,
the points `(x$x[i], x$y[i])`

for which
`subset[i]=TRUE`

will be retained, and the others
will be deleted.

The pattern will be ``trimmed''
if `i`

is an object of class
`"owin"`

specifying a window of observation.
The points of `x`

lying inside the new
window `i`

will be retained. Alternatively `i`

may be a
pixel image (object of class `"im"`

) with logical values;
the pixels with the value `TRUE`

will be interpreted as a window.

The argument `drop`

determines whether to remove
unused levels of a factor, if the point pattern is multitype
(i.e. the marks are a factor) or if the marks are a data frame
in which some of the columns are factors.

The function `[<-.ppp`

is a method for `[<-`

for the
class `"ppp"`

. It replaces the designated
subset with the point pattern `value`

.
The subset of `x`

to be replaced is designated by
the argument `i`

as above.

The replacement point pattern `value`

must lie inside the
window of the original pattern `x`

.
The ordering of points in `x`

will be preserved
if the replacement pattern `value`

has the same number of points
as the subset to be replaced. Otherwise the ordering is
unpredictable.

If the original pattern `x`

has marks, then the replacement
pattern `value`

must also have marks, of the same type.

Use the function `unmark`

to remove marks from a
marked point pattern.

Use the function `split.ppp`

to select those points
in a marked point pattern which have a specified mark.

##### Value

A point pattern (of class `"ppp"`

).

##### Warnings

The function does not check whether `i`

is a subset of
`Window(x)`

. Nor does it check whether `value`

lies
inside `Window(x)`

.

##### See Also

##### Examples

```
# NOT RUN {
# Longleaf pines data
lon <- longleaf
# }
# NOT RUN {
plot(lon)
# }
# NOT RUN {
# }
# NOT RUN {
# adult trees defined to have diameter at least 30 cm
longadult <- subset(lon, marks >= 30)
# }
# NOT RUN {
plot(longadult)
# }
# NOT RUN {
# note that the marks are still retained.
# Use unmark(longadult) to remove the marks
# New Zealand trees data
# }
# NOT RUN {
plot(nztrees) # plot shows a line of trees at the far right
abline(v=148, lty=2) # cut along this line
# }
# NOT RUN {
nzw <- owin(c(0,148),c(0,95)) # the subwindow
# trim dataset to this subwindow
nzsub <- nztrees[nzw]
# }
# NOT RUN {
plot(nzsub)
# }
# NOT RUN {
# Redwood data
# }
# NOT RUN {
plot(redwood)
# }
# NOT RUN {
# Random thinning: delete 60% of data
retain <- (runif(npoints(redwood)) < 0.4)
thinred <- redwood[retain]
# }
# NOT RUN {
plot(thinred)
# }
# NOT RUN {
# Scramble 60% of data
X <- redwood
modif <- (runif(npoints(X)) < 0.6)
X[modif] <- runifpoint(ex=X[modif])
# Lansing woods data - multitype points
lan <- lansing
# }
# NOT RUN {
# Hickory trees
hicks <- split(lansing)$hickory
# Trees in subwindow
win <- owin(c(0.3, 0.6),c(0.2, 0.5))
lsub <- lan[win]
# Scramble the locations of trees in subwindow, retaining their marks
lan[win] <- runifpoint(ex=lsub) %mark% marks(lsub)
# Extract oaks only
oaknames <- c("redoak", "whiteoak", "blackoak")
oak <- lan[marks(lan) %in% oaknames, drop=TRUE]
oak <- subset(lan, marks %in% oaknames, drop=TRUE)
# To clip or not to clip
X <- runifpoint(25, letterR)
B <- owin(c(2.2, 3.9), c(2, 3.5))
opa <- par(mfrow=c(1,2))
plot(X, main="X[B]")
plot(X[B], border="red", cols="red", add=TRUE, show.all=TRUE, main="")
plot(X, main="X[B, clip=TRUE]")
plot(B, add=TRUE, lty=2)
plot(X[B, clip=TRUE], border="blue", cols="blue", add=TRUE,
show.all=TRUE, main="")
par(opa)
# }
```

*Documentation reproduced from package spatstat, version 1.55-1, License: GPL (>= 2)*