replace_tail: replace_tail

View source: R/replace_tail.R

replace_tailR Documentation

replace_tail

Description

Replace final n values in a vector.

Usage

replace_tail(v, n, r)

Arguments

v

vector. Input vector to have tail replaced.

n

integer. Number of values at tail of v to replace. If left NULL, this will be calculated as the length of r.

r

String, logical, numeric etc. What to use as replacement. Should either be a single value or vector of length n.

Details

A simple solution to the lack of an elegant way in R of simply replacing the last n values in a vector. There are lots of ways of extracting the last n values, and a few of these can be used to replace them, but they are all not very elegant in code. There is a similar function for the (much easier) operation to replace the initial n values: replace_head().

Value

The v vector is returned but with the final n values overwritten by r.

Author(s)

Nicholas Carey - nicholascarey@gmail.com

See Also

replace_head

Examples

# Replace last 5 numeric values with single value
x <- 1:10
replace_tail(x, 5, 100)

# Replace last logical with a different logical
x <- rep(TRUE, 10)
replace_tail(x, 1, FALSE)

# Replace last n with a vector of same length
x <- 1:20
replace_tail(x, 5, c(100,99,98,97,96))


nicholascarey/caRey documentation built on Sept. 30, 2023, 3:47 a.m.