replace_tail | R Documentation |
Replace final n
values in a vector.
replace_tail(v, n, r)
v |
vector. Input vector to have tail replaced. |
n |
integer. Number of values at tail of |
r |
String, logical, numeric etc. What to use as replacement. Should
either be a single value or vector of length |
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()
.
The v
vector is returned but with the final n
values overwritten
by r
.
Nicholas Carey - nicholascarey@gmail.com
replace_head
# 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))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.