sparsevctrs

Sparse vector class using ALTREP

https://github.com/r-lib/sparsevctrs

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.6%) to scientific vocabulary
Last synced: 10 months ago · JSON representation

Repository

Sparse vector class using ALTREP

Basic Info
Statistics
  • Stars: 21
  • Watchers: 3
  • Forks: 1
  • Open Issues: 9
  • Releases: 6
Created over 3 years ago · Last pushed about 1 year ago
Metadata Files
Readme Changelog License

README.Rmd

---
output: github_document
---



```{r}
#| include: false
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)
set.seed(1234)
```

# sparsevctrs sparsevctrs website


[![R-CMD-check](https://github.com/r-lib/sparsevctrs/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/r-lib/sparsevctrs/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/r-lib/sparsevctrs/graph/badge.svg)](https://app.codecov.io/gh/r-lib/sparsevctrs)


The goal of sparsevctrs is to provide a sparse vector [ALTREP](https://svn.r-project.org/R/branches/ALTREP/ALTREP.html) class. With this, you can have sparse data in the form of sparse columns in `data.frame` or [tibble](https://tibble.tidyverse.org/). Due to the nature of how ALTREP vectors work, these sparse vectors will behave like the normal dense vectors you are used you. The vectors will contain their sparseness as much as they can, and only materialize when they have to.

## Installation

You can install the development version of sparsevctrs like so:

``` r
remotes::install_github("r-lib/sparsevctrs")
```

## Examples

A sparse vector, here specifically a sparse double vector, will be identical to its dense counterpart, often with a smaller memory footprint.

```{r}
library(sparsevctrs)
library(lobstr)

x_sparse <- sparse_double(value = c(3, 1, 10), position = c(2, 7, 15), length = 1000)
x_dense <- numeric(1000)
x_dense[2] <- 3
x_dense[7] <- 1
x_dense[15] <- 10

obj_size(x_sparse)
obj_size(x_dense)

identical(x_sparse, x_dense)
```

The memory of a sparse vector is proportional to the number of elements plus a constant. This means that increasing the length of a sparse vector doesn't increase how much memory it uses. Unlike dense vectors who has a much smaller constant, but increases according to the length of the values. 

```{r}
x_sparse_0 <- sparse_double(numeric(), integer(), length = 0)
x_sparse_1000 <- sparse_double(numeric(), integer(), length = 1000)
x_sparse_1000000 <- sparse_double(numeric(), integer(), length = 10000000)

obj_size(x_sparse_0)
obj_size(x_sparse_1000)
obj_size(x_sparse_1000000)

x_dense_0 <- numeric(0)
x_dense_1000 <- numeric(1000)
x_dense_1000000 <- numeric(10000000)

obj_size(x_dense_0)
obj_size(x_dense_1000)
obj_size(x_dense_1000000)
```

These sparse vectors are compatible with tibbles and data frames.

```{r}
library(tibble)
set.seed(1234)

tibble(
  x = sample(1:1000),
  y = sparse_double(1, 7, 1000)
)
```

## Motivation

Sparse data happens from ingestion and preprocessing calculations. text to counts, dummy variables etc etc

There are computational tools for calculations using sparse matrices, specifically the Matrix package and some modeling packages (e.g., xgboost, glmnet, etc.). We want to utilize these tools as best we can without making redundant implementations.

However, sparse matrices are not great for data in general, or at least not until the very end, when mathematical calculations occur. Converting everything to “numeric” is problematic for dates, factors, etc. There are good reasons why data frames were created in the first place. Matrices are efficient but primitive.

The problem is that many tools, especially the tidyverse, rely on data frames since they are more expressive and accommodate different variable types. We need to merge and filter rows/columns, etc, in a flexible and user-friendly way. (joins, pivoting)

Having a sparse representation of data that allows us to use modern data manipulation interfaces, keeps memory overhead low, and can be efficiently converted to a more primitive matrix format so that we can let Matrix and other packages do what they do best.

This is achieved with this package, by providing sparse vectors that fit into a data frame. Along with converting tools between sparse matrices and data frames.

Owner

  • Name: R infrastructure
  • Login: r-lib
  • Kind: organization

GitHub Events

Total
  • Create event: 30
  • Release event: 5
  • Issues event: 21
  • Watch event: 12
  • Delete event: 21
  • Issue comment event: 3
  • Push event: 77
  • Pull request review comment event: 1
  • Pull request review event: 2
  • Pull request event: 48
Last Year
  • Create event: 30
  • Release event: 5
  • Issues event: 21
  • Watch event: 12
  • Delete event: 21
  • Issue comment event: 3
  • Push event: 77
  • Pull request review comment event: 1
  • Pull request review event: 2
  • Pull request event: 48

Issues and Pull Requests

Last synced: 10 months ago

All Time
  • Total issues: 42
  • Total pull requests: 68
  • Average time to close issues: 23 days
  • Average time to close pull requests: about 13 hours
  • Total issue authors: 1
  • Total pull request authors: 2
  • Average comments per issue: 0.4
  • Average comments per pull request: 0.01
  • Merged pull requests: 63
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 13
  • Pull requests: 32
  • Average time to close issues: 6 days
  • Average time to close pull requests: about 7 hours
  • Issue authors: 1
  • Pull request authors: 1
  • Average comments per issue: 0.08
  • Average comments per pull request: 0.03
  • Merged pull requests: 29
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • EmilHvitfeldt (22)
Pull Request Authors
  • EmilHvitfeldt (80)
  • DavisVaughan (1)
Top Labels
Issue Labels
feature (9) bug (2) Investigate (1) upkeep (1)
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • cran 142,186 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 0
  • Total versions: 7
  • Total maintainers: 1
cran.r-project.org: sparsevctrs

Sparse Vectors for Use in Data Frames

  • Versions: 7
  • Dependent Packages: 0
  • Dependent Repositories: 0
  • Downloads: 142,186 Last month
Rankings
Dependent packages count: 28.8%
Dependent repos count: 35.5%
Average: 49.9%
Downloads: 85.4%
Maintainers (1)
Last synced: 11 months ago

Dependencies

.github/workflows/pkgdown.yaml actions
  • JamesIves/github-pages-deploy-action v4.4.1 composite
  • actions/checkout v3 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
DESCRIPTION cran
  • Matrix * imports
  • tibble * imports
  • vctrs * imports
  • rlang * suggests
  • rsparse * suggests
.github/workflows/R-CMD-check.yaml actions
  • actions/checkout v4 composite
  • r-lib/actions/check-r-package v2 composite
  • r-lib/actions/setup-pandoc v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/pr-commands.yaml actions
  • actions/checkout v4 composite
  • r-lib/actions/pr-fetch v2 composite
  • r-lib/actions/pr-push v2 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite
.github/workflows/test-coverage.yaml actions
  • actions/checkout v4 composite
  • actions/upload-artifact v4 composite
  • r-lib/actions/setup-r v2 composite
  • r-lib/actions/setup-r-dependencies v2 composite