swap: Swap Elements in a Vector

Description Usage Arguments Details See Also Examples

View source: R/swap.R View source: R/swap.R

Description

This function swaps elements in a vector. See examples for usage.

Usage

1
swap(vec, from, to = names(from))

Arguments

vec

the vector of items whose elements you will be replacing.

from

the items you will be mapping 'from'.

to

the items you will be mapping 'to'. must be same length and order as from.

Details

If to is of different type than from, it will be coerced to be of the same type.

See Also

swap_

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
x <- c(1, 2, 2, 3)
from <- c(1, 2)
to <- c(10, 20)
swap( x, from, to )

## alternatively, we can submit a named character vector
## we translate from value to name. note that this forces
## a conversion to character
names(from) <- to
swap( x, from )

## NAs are handled sensibly. Types are coerced as needed.
x <- c(1, NA, 2, 2, 3)
swap(x, c(1, 2), c("a", "b") )

Example output

[1] 10 20 20  3
[1] "10" "20" "20" "3" 
[1] "a" NA  "b" "b" "3"

Kmisc documentation built on May 29, 2017, 1:43 p.m.

Related to swap in Kmisc...