swap: function to swap values in a matrix or grid with other values

View source: R/swap.R

swapR Documentation

function to swap values in a matrix or grid with other values

Description

This function returns an object of the same type and dimension as x in which all occurrances of values in original have been replaced by the corresponding value in replacement.

Usage

swap(x, original, replacement, ...)
## S3 method for class 'grid'
swap(x, original, replacement, ...)
## S3 method for class 'matrix'
swap(x, original, replacement, no.match=NA, na.value=NA, ...)

Arguments

x

a grid or matrix object

original

A vector of values to be replaced.

replacement

A vector of the replacement values.

no.match

Non-NA values in x that don't match any value in original will be replaced with this value.

na.value

NA values in x will be replaced with this value.

...

Arguments to be passed to other methods; for the grid method this might be na.value or no.match

Value

The function returns an object of the same class as x in which all occurrances of values in original are swapped with their value in replacement.

Note

Any value that isn't in original will be replaced with no.match unless it is NA in which case it will be replaced with na.value. Note it is also possible to include an NA value in original and a corresponding replacement value in replacement which will take precedence over na.value.

Author(s)

Ethan Plunkett

Examples


# Note it is possible to swap numeric values with characters and vice versa:
# (However, character grids cannot be written to disk!)
m <- matrix(c(1:11, NA), 4, 3)


# Values not in original will be replaced with NA. 
swap(m, 1:6, 11:16)

# Replace non-matching with 1
swap(m, 1:6, 11:16,  no.match = 1)

# Replace non-maching with 1 and na with 0
swap(m, 1:6, 11:16,  no.match = 1,  na.value = 0)

# It's typical to work directly with grids
g <- asgrid(m, xll = 0, yll = 0, cellsize = 10)
g2 <- swap(g, 1:6, 11:16)






ethanplunkett/gridprocess documentation built on Feb. 1, 2024, 2:24 a.m.