sk_sub | R Documentation |
Creates a "sk" object containing only the grid-lines specified in idx_keep
. Alternatively,
grid lines to remove can be specified in idx_rem
.
sk_sub(g, ij_keep = NULL, ij_rem = NULL, idx = FALSE, mirror = FALSE)
g |
sk grid or any grid-like object accepted by |
ij_keep |
list of grid line numbers ("i" and "j") forming regular sub-grid |
ij_rem |
list of grid line numbers ("i" and "j") whose exclusion forms regular sub-grid |
idx |
logical, if TRUE the function returns a list containing |
mirror |
logical, whether to mirror the selection in |
One of idx_keep
or idx_rem
(but not both) can be specified, and the grid line numbers
(not intercepts) should be supplied in ascending order in list entries named "i" and "j".
If idx_rem
is specified, mirror=TRUE
will cause the selection in idx_rem
to be
reflected about the central grid line (useful for specifying outer grid lines). mirror
is ignored if idx_keep
is specified instead.
Default idx=FALSE
causes the function to return the sub-grid as a sk grid object.
If idx=TRUE
, the function instead returns a list containing idx_keep
and idx_rem
as
specified above.
If neither idx_keep
nor idx_rem
is supplied, the function removes outer grid lines
iteratively (selecting the one with highest proportion of NA
s), attempting to find a
complete sub-grid (having no NA
s) somewhere in the interior. This heuristic is designed
for rasters with few NA
s, all located around the perimeter.
an sk grid object, the requested sub-grid of g
Other sk constructors:
sk_rescale()
,
sk_snap()
,
sk()
# make an example grid
g = sk(c(50, 100))
g[] = apply(expand.grid(g[['gyx']]), 1, \(z) cos( 2*sum(z^2) ) )
plot(g)
# subset by specifying grid lines to keep
ij_keep = list(i=seq(1, 50, by=2), j=seq(1, 50, by=2))
g_keep = sk_sub(g, ij_keep)
plot(g_keep)
# get the indices kept and removed
idx = sk_sub(g, ij_keep, idx=TRUE)
# equivalent call specifying grid lines to omit
g_rem = sk_sub(g, ij_rem=idx[['rem']])
identical(g_rem, g_keep)
# remove some data around the edges of the grid
idx = sk_sub(g, ij_rem=list(i=seq(10), j=seq(10)), mirror=TRUE, idx=TRUE)
idx_y_pts = sk_sub_idx(dim(g), idx[['rem']]['i'], idx=TRUE)
idx_x_pts = sk_sub_idx(dim(g), idx[['rem']]['j'], idx=TRUE)
idx_pts = c(idx_y_pts, idx_x_pts)
idx_na = sort(sample(idx_pts, 0.6*length(idx_pts)))
g[idx_na] = NA
plot(g)
# identify the interior sub-grid that is complete
g_sub = sk_sub(g)
print(g_sub)
plot(g_sub)
# verify it is as large as expected
( dim(g) - dim(g_sub) ) == sapply(idx[['rem']], length)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.