Construct Mixed Type Data Structures with Vectors of Vectors

Mixed type vectors are useful for combining semantically similar classes. Some examples of semantically related classes include time across different granularities (e.g. daily, monthly, annual) and probability distributions (e.g. Normal, Uniform, Poisson). These groups of vector types typically share common statistical operations which vary in results with the attributes of each vector. The 'vecvec' data structure facilitates efficient storage and computation across multiple vectors within the same object.


vecvec

Lifecycle:stable R-CMD-check CRANstatus

The vecvec package allows you to create vectors of mixed classes. Mixed type vectors are useful for data analysis packages as they allow semantically similar classes (such as time or distributions) to be stored within the same vector and operated upon efficiently with the same functions. It works similarly to factor(), in which the values of a vector (analogous to levels of a factor) are stored in the attributes. This also allows for efficient computation when the values are replicated, since operations apply to the unique values of the vector.

This package is intended for developers creating mixed type vector packages, and is unlikely useful for general data analysis (although I’d be curious if you find direct application of this tool, let me know how you use it).

Installation

You can install the stable version from CRAN:

install.packages("vecvec")

You can install the development version of vecvec like so:

remotes::install_github("mitchelloharawild/vecvec")

Example

Use vecvec() to create a vecvec. Here we combine three vectors of different types.

library(vecvec)

x <- vecvec(as.Date("2025-03-29"), as.POSIXct("2025-01-03 10:05:00"), rnorm(10))
x
#> <vecvec[12]>
#>  [1] 2025-03-29          2025-01-03 10:05:00  1.262954285       
#>  [4] -0.326233361         1.329799263         1.272429321       
#>  [7]  0.414641434        -1.539950042        -0.928567035       
#> [10] -0.294720447        -0.005767173         2.404653389

Internally, the vectors are stored separately in the attributes.

str(x)
#> <vecvec::vecvec> int [1:12] 1 2 3 4 5 6 7 8 9 10 ...
#>  - attr(*, "vecvec_x")=List of 3
#>   ..$ : Date[1:1], format: "2025-03-29"
#>   ..$ : POSIXct[1:1], format: "2025-01-03 10:05:00"
#>   ..$ : num [1:10] 1.263 -0.326 1.33 1.272 0.415 ...
#>  @ x: List of 3
#>  .. $ : Date[1:1], format: "2025-03-29"
#>  .. $ : POSIXct[1:1], format: "2025-01-03 10:05:00"
#>  .. $ : num [1:10] 1.263 -0.326 1.33 1.272 0.415 ...

Operations apply to all vector types.

x + 1
#> <vecvec[12]>
#>  [1] 2025-03-30          2025-01-03 10:05:01  2.26295428        
#>  [4]  0.67376664          2.32979926          2.27242932        
#>  [7]  1.41464143         -0.53995004          0.07143297        
#> [10]  0.70527955          0.99423283          3.40465339

When subsetting, unused values are dropped.

x[1]
#> <date*[1]>
#> [1] 2025-03-29
str(x[1])
#> <vecvec::vecvec> num 20176
#>  - attr(*, "vecvec_ptype")= 'Date' num(0) 
#>  @ x: List of 1
#>  .. $ : Date[1:1], format: "2025-03-29"

When a vecvec is replicated, only the indexing is replicated.

str(rep(x, each = 10))
#> <vecvec::vecvec> int [1:120] 1 1 1 1 1 1 1 1 1 1 ...
#>  - attr(*, "vecvec_x")=List of 3
#>   ..$ : Date[1:1], format: "2025-03-29"
#>   ..$ : POSIXct[1:1], format: "2025-01-03 10:05:00"
#>   ..$ : num [1:10] 1.263 -0.326 1.33 1.272 0.415 ...
#>  @ x: List of 3
#>  .. $ : Date[1:1], format: "2025-03-29"
#>  .. $ : POSIXct[1:1], format: "2025-01-03 10:05:00"
#>  .. $ : num [1:10] 1.263 -0.326 1.33 1.272 0.415 ...

The values in the attribute are unchanged, which allows for efficient computation since operations apply to the attributes (which are not replicated).

str(rep(x, each = 10) + 100)
#> <vecvec::vecvec> int [1:120] 1 1 1 1 1 1 1 1 1 1 ...
#>  - attr(*, "vecvec_x")=List of 3
#>   ..$ : Date[1:1], format: "2025-07-07"
#>   ..$ : POSIXct[1:1], format: "2025-01-03 10:06:40"
#>   ..$ : num [1:10] 101.3 99.7 101.3 101.3 100.4 ...
#>  @ x: List of 3
#>  .. $ : Date[1:1], format: "2025-07-07"
#>  .. $ : POSIXct[1:1], format: "2025-01-03 10:06:40"
#>  .. $ : num [1:10] 101.3 99.7 101.3 101.3 100.4 ...

If the arguments is differ across replicated indices, the resulting vecvec is expanded

str(rep(x, each = 10) + 1:120)
#> <vecvec::vecvec> int [1:120] 1 2 3 4 5 6 7 8 9 10 ...
#>  - attr(*, "vecvec_x")=List of 3
#>   ..$ : Date[1:10], format: "2025-03-30" "2025-03-31" ...
#>   ..$ : POSIXct[1:10], format: "2025-01-03 10:05:11" "2025-01-03 10:05:12" ...
#>   ..$ : num [1:100] 22.3 23.3 24.3 25.3 26.3 ...
#>  @ x: List of 3
#>  .. $ : Date[1:10], format: "2025-03-30" "2025-03-31" ...
#>  .. $ : POSIXct[1:10], format: "2025-01-03 10:05:11" "2025-01-03 10:05:12" ...
#>  .. $ : num [1:100] 22.3 23.3 24.3 25.3 26.3 ...

Single-type vecvecs

A vecvec whose elements all share one type is stored as an ordinary vector of those values, so it works like the plain vector, including in base R modelling functions.

y <- vecvec(c(2.5, 1, 4))
str(y)
#> <vecvec::vecvec> num [1:3] 2.5 1 4
#>  - attr(*, "vecvec_ptype")= num(0) 
#>  @ x: List of 1
#>  .. $ : num [1:3] 2.5 1 4
coef(lm(c(1, 2, 3) ~ y))
#> (Intercept)           y 
#>   1.1666667   0.3333333

Models need values of a single type, so they error for a mixed-type vecvec (and for a vecvec of factors, whose class models rely on). Use unvecvec() to convert it first.

z <- vecvec(1:2, 0.5)
lm(c(1, 2, 3) ~ z)
#> Error:
#> ! Can't use `z` in a model: it is a mixed-type vecvec.
#> ℹ It holds values of 2 different types: <integer> and <double>.
#> ℹ Models need values of a single type. Convert it first, e.g. with
#>   `unvecvec()`.
coef(lm(c(1, 2, 3) ~ unvecvec(z)))
#> (Intercept) unvecvec(z) 
#>   2.5000000  -0.4285714

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("vecvec")

2.0.0 by Mitchell O'Hara-Wild, 3 days ago


https://pkg.mitchelloharawild.com/vecvec/, https://github.com/mitchelloharawild/vecvec


Report a bug at https://github.com/mitchelloharawild/vecvec/issues


Browse source code at https://github.com/cran/vecvec


Authors: Mitchell O'Hara-Wild [aut, cre] (ORCID:


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports cli, rlang, S7, stats, utils, vctrs

Suggests data.table, knitr, lifecycle, rmarkdown, testthat, tibble, tidyr


Imported by ggtime, mixtime.


See at CRAN