sub-.flexseq: Flexseq Indexing

$.flexseqR Documentation

Flexseq Indexing

Description

Index, replace, and extract elements of a flexseq by position or name.

Usage

## S3 method for class 'flexseq'
x$name

## S3 replacement method for class 'flexseq'
x$name <- value

## S3 method for class 'flexseq'
x[i, ...]

## S3 method for class 'flexseq'
x[[i, ...]]

## S3 replacement method for class 'flexseq'
x[i] <- value

## S3 replacement method for class 'flexseq'
x[[i]] <- value

Arguments

x

A flexseq.

name

Element name (for $ and ⁠$<-⁠).

value

Replacement values; recycled to selected index length.

i

Positive integer indices, character element names, or logical mask. For [[, a single integer or character name.

...

Unused.

Details

Selector behavior:

  • Integer indexing ([) returns elements in the requested order.

  • Character indexing ([) returns elements in requested name order; unknown names become NULL elements in the output.

  • Logical indexing ([) is positional and follows logical-mask selection.

Replacement behavior:

  • ⁠[<-⁠ is persistent and recycles value to the number of selected positions (with standard R recycling warnings where applicable).

  • ⁠[[<-⁠ replaces one element; assigning NULL removes that element.

Value

For $: the matched element.

For ⁠$<-⁠: updated tree with one named element replaced.

For [: a new flexseq containing selected elements in query order. For character indexing, missing names are represented as NULL elements.

For [[: the extracted element (internal name metadata is removed).

For ⁠[<-⁠: a new flexseq with selected elements replaced.

For ⁠[[<-⁠: a new flexseq with one element replaced.

Examples


# $ extracts by name
x <- as_flexseq(setNames(as.list(1:3), c("a", "b", "c")))
x$b

# $<- replaces by name
x <- as_flexseq(setNames(as.list(1:3), c("a", "b", "c")))
x$b <- 20
x$b
x <- as_flexseq(letters[1:6])
x

x2 <- x[c(2, 4, 6)]
x2

# named lookups return NULL for missing names
x3 <- as_flexseq(setNames(as.list(letters[1:4]), c("w", "x", "y", "z")))
x4 <- x3[c("y", "missing", "w")]
x4

# logical indexing supports recycling
x[c(TRUE, FALSE)]

# [[ extracts one element
x <- as_flexseq(letters[1:5])
x[[3]]

x2 <- as_flexseq(setNames(as.list(letters[1:3]), c("a1", "a2", "a3")))
x2[["a2"]]

# [<- replaces selected elements
x <- as_flexseq(1:6)
x

x2 <- x
x2[c(2, 5)] <- list(20, 50)
x2

# character replacement uses element names
x3 <- as_flexseq(setNames(as.list(1:4), c("a", "b", "c", "d")))
x3[c("d", "a")] <- list(40, 10)
x3

# logical replacement supports recycling
x4 <- x
x4[c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE)] <- list(1)
x4

# [[<- replaces one element
x <- as_flexseq(letters[1:4])
x2 <- x
x2[[2]] <- "ZZ"
x2

x3 <- as_flexseq(setNames(as.list(1:3), c("x", "y", "z")))
x3[["y"]] <- 99
x3

# assigning NULL removes one element
x4 <- as_flexseq(letters[1:4])
x4[[2]] <- NULL
x4

Immutables documentation built on April 29, 2026, 1:06 a.m.