Learn R Programming

Waypoint (version 2.0.1)

Extract: Extract or Replace Parts of a Coords Object

Description

Extract or replace subsets of coords.

Usage

# S3 method for coords
[(x, i)

# S3 method for coords [(x, i) <- value

Value

A "coords" object.

Arguments

x

a "coords" object.

i

indices specifying elements to extract or replace—see base Extract.

value

a numeric, a numeric vector of coordinate values of length(i), or a "coords" object, possibly named.

Details

Subsetting a "coords" object (except by an empty index) will drop all attributes except fmt, latlon, names and valid. Indices referencing values greater than length(x) will throw a subscript out of bounds error. If names are not required, use unname(), see examples.

Replacement values may be a single numeric, a numeric vector of coordinate values of length(i), or a "coords" object, possibly with a "latlon" attribute. However, the "latlon" attribute of the replacement value is ignored if the "coords" object x has no corresponding attribute set. If replacement values are named, the names are also ignored; to replace names, use names<-() replacement form.

The [<-(<coords>) replacement operator automatically revalidates "coords" objects after the replacement operation by invoking validate().

See Also

"coords", base Extract, unname(), and validate().

Examples

Run this code
## Continuing example from `as_coords()`...
# \dontshow{
   dm <-
       c(5130.4659, 4932.7726, 4806.4339, 3853.3696, 0.0000, -3706.7044, -5306.2869, -2514.4093,
         -007.6754, 1823.9137, -12246.7203, -7702.1145, 0.0000, -1217.3178, 7331.0370, -5731.1536)
   names(dm) <- 
       rep(c("Nelson's Column", "Ostravice", "Tally Ho", "Washington Monument", "Null Island",
             "Tristan da Cunha", "Mawson Peak", "Silvio Pettirossi International Airport"), 2)
   invisible(as_coords(dm, fmt = 2))
   latlon(dm) <- rep(c(TRUE, FALSE), each = 8)
# }

## Named "coords" object in degrees and minutes with
## eight values each of latitude and longitude
dm

## Extract the first eight values
dm[1:8]

## Exclude the first eight values
dm[-8:0]

## Index odd-numbered values
(index <- as.logical(1:16 %% 2))
dm[index]

## Extract values without names
unname(dm)[1:4]

## Create "coords" object with updated position of Tally Ho
newpos <- as_coords(c(4930.342, -12411.580), fmt = 2)
latlon(newpos) <- c(TRUE, FALSE)
newpos

## Update position using the "coords" object as replacement value
dm[c(3, 11)] <- newpos
dm[c(3, 11)]

## Or, as latlon didn't actually change, use simple numeric vector
dm[c(3, 11)] <- c(4930.342, -12411.580)
dm[c(3, 11)]

rm(dm, index, newpos)

Run the code above in your browser using DataLab