lset: Set values in an lvec

View source: R/lset.R

lsetR Documentation

Set values in an lvec

Description

Set values in an lvec

Usage

lset(x, ...)

## S3 method for class 'lvec'
lset(x, index = NULL, values, range = NULL, ...)

## Default S3 method:
lset(x, index = NULL, values, range = NULL, ...)

## S3 method for class 'data.frame'
lset(x, index = NULL, values, range = NULL, ...)

Arguments

x

lvec to set values in

...

used to pass additional arguments to other methods

index

a numeric or logical vector with indices at which the values should be set.

values

a vector with the new values. When shorter than the length of the indices the values are recycled.

range

a numeric vector of length 2 specifying a range of elements to select. Specify either index or range.

Details

Should behave in the same way as assigning and indexing to a regular R-vector. The range given by range includes both end elements. So, a range of c(1,3) selects the first three elements.

When range is given, and values is not given it is assumed index contains the values. Therefore, one can do lset(x, range = c(1,4), NA), to set the first four elements of x to missing.

Examples

a <- as_lvec(1:10)
# set second element to 20
lset(a, 2, 20)
print(a)
# set odd elements to 20
lset(a, c(TRUE, FALSE), 20)
print(a)
# values are recycled
lset(a, 1:4, 100:101)
print(a)
# range index; set first 3 elements to NA
lset(a, range = c(1,3), NA)
print(a)


djvanderlaan/lvec documentation built on Oct. 26, 2022, 11:43 a.m.