Skip to contents

class_vecvec() constructs a new vecvec object from a list of vectors. It is designed to be performant with minimal checks on the inputs. Direct use of class_vecvec() is useful for developers, but for users it is recommended to use vecvec() to create vecvec objects.

Usage

class_vecvec(x = list(), i = seq_len(sum(list_sizes(x))))

Arguments

x

An unnamed list of arbitrary vectors. The vectors can be of different types and lengths, but they must all be vectors (according to vctrs::vec_is). Vectors of the same type are combined together into a single vector.

i

A vector of integers specifying the location of each element in x as if they were combined in order. The values in i must be between 1 and the total number of elements across all vectors in x, and can contain duplicates. If not provided, it defaults to a sequence from 1 to the total number of elements across all vectors in x.

Value

A vector of vectors of S7 object of class class_vecvec.

Details

A vecvec is stored in one of two forms, chosen automatically from its contents:

  • single-type: every element has the same type, held in an atomic vector (numbers, strings, dates, ...). The vecvec's data is that vector's data, so base R code that reads it directly (such as lm() or model.matrix()) sees its values.

  • mixed-type: anything else. The vecvec's data is an integer index into the stored vectors. Modelling functions error for mixed-type vecvecs rather than fit on the index.

Vectors of the same type (with identical attributes) are always combined, so a vecvec is single-type exactly when all of its elements share one type.

vctrs treats a single-type vecvec as a vector of its type, and a mixed-type vecvec as a vector of any types. So, as for any other vctrs type:

  • Combining vecvecs (e.g. with vctrs::vec_c() or dplyr::bind_rows()) gives a single-type vecvec when they all have the same type, and a mixed-type vecvec otherwise.

  • Casting to a single-type vecvec casts the values to its type, so vctrs::vec_assign() (and functions using it, like tidyr::replace_na()) can't put values of another type into a single-type vecvec. Base [<- can, and gives a mixed-type vecvec.

The same holds for subclasses (see vecvec_register()).

Examples

# Create a vecvec prototype
class_vecvec()
#> <vecvec[0]>

# Construct a vecvec from a list of vectors
class_vecvec(list(letters, rnorm(10)))
#> <vecvec[36]>
#>  [1] a            b            c            d            e           
#>  [6] f            g            h            i            j           
#> [11] k            l            m            n            o           
#> [16] p            q            r            s            t           
#> [21] u            v            w            x            y           
#> [26] z            -1.400043517  0.255317055 -2.437263611 -0.005571287
#> [31]  0.621552721  1.148411606 -1.821817661 -0.247325302 -0.244199607
#> [36] -0.282705449

# Fully specify a vecvec with locations
class_vecvec(
  x = list(letters, rnorm(10)),
  i = c(1:3, 27:31, 26:4, 32:36)
)
#> <vecvec[36]>
#>  [1] a           b           c           -0.55369938  0.62898204  2.06502490
#>  [7] -1.63098940  0.51242695 z           y           x           w          
#> [13] v           u           t           s           r           q          
#> [19] p           o           n           m           l           k          
#> [25] j           i           h           g           f           e          
#> [31] d           -1.86301149 -0.52201251 -0.05260191  0.54299634 -0.91407483