forward operations to vectorised attributes

Open
#2,948 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
r
Domain
data

Research direction

Reproduce the data.table examples with the errors package, especially the x[...] subset operation. Start at the errors class's subsetting operator and trace how data.table applies row filters to the column; done means the selected values and their error metadata have matching lengths without warnings or corruption.

Written by the indexing model from the issue text.

Description

non-atomic column

The units package stores unit metadata for vectors and arrays as an attribute, and it seems to work fine with data.table:

library(data.table)
library(units)
#> udunits system database from /usr/share/udunits

(DT <- data.table(x = set_units(1:5, m), y = set_units(2, s)))
#>      x   y
#> 1: 1 m 2 s
#> 2: 2 m 2 s
#> 3: 3 m 2 s
#> 4: 4 m 2 s
#> 5: 5 m 2 s
DT[, z := x/y]
DT
#>      x   y       z
#> 1: 1 m 2 s 0.5 m/s
#> 2: 2 m 2 s 1.0 m/s
#> 3: 3 m 2 s 1.5 m/s
#> 4: 4 m 2 s 2.0 m/s
#> 5: 5 m 2 s 2.5 m/s
DT[x > set_units(3, m)]
#>      x   y       z
#> 1: 4 m 2 s 2.0 m/s
#> 2: 5 m 2 s 2.5 m/s

Similarly, the errors package stores uncertainty metadata for vectors and arrays, but this time the attribute is a vector of errors. Some operations are correctly forwarded:

library(errors)

(DT <- data.table(x = set_errors(1:5, 1:5/10), y = set_errors(2, 0.1)))
#>         x      y
#> 1: 1.0(1) 2.0(1)
#> 2: 2.0(2) 2.0(1)
#> 3: 3.0(3) 2.0(1)
#> 4: 4.0(4) 2.0(1)
#> 5: 5.0(5) 2.0(1)
DT[, z := x/y]
DT
#>         x      y       z
#> 1: 1.0(1) 2.0(1) 0.50(6)
#> 2: 2.0(2) 2.0(1)  1.0(1)
#> 3: 3.0(3) 2.0(1)  1.5(2)
#> 4: 4.0(4) 2.0(1)  2.0(2)
#> 5: 5.0(5) 2.0(1)  2.5(3)

As you can see above, errors are propagated for the division. But others not:

(DT.subset <- DT[x > set_errors(3)])
#> Warning: In '>' : boolean operators not defined for 'errors' objects,
#> errors dropped
#> Warning in exponent + value_digits: longer object length is not a multiple
#> of shorter object length
#> Warning in (scientific | (exponent > 4 + scipen | exponent < -3 - scipen))
#> & : longer object length is not a multiple of shorter object length
#> Warning in exponent + value_digits: longer object length is not a multiple
#> of shorter object length
#> Warning in (scientific | (exponent > 4 + scipen | exponent < -3 - scipen))
#> & : longer object length is not a multiple of shorter object length
#> Warning in exponent + value_digits: longer object length is not a multiple
#> of shorter object length
#> Warning in (scientific | (exponent > 4 + scipen | exponent < -3 - scipen))
#> & : longer object length is not a multiple of shorter object length
#> Error in dimnames(x) <- dn: length of 'dimnames' [1] not equal to array extent

The errors class does implement the subsetting operator, but it's not getting called, so the resulting errors object is corrupted (more errors than values):

DT$x
#> Errors: 0.1 0.2 0.3 0.4 0.5
#> [1] 1 2 3 4 5
DT.subset$x
#> Errors: 0.1 0.2 0.3 0.4 0.5
#> [1] 4 5
Dominant language
R
Stars
3.9k
Forks
1.1k
Avg merge
14h 4m
Merged PRs (30d)
4

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Rdatatable/data.table

All issues in Rdatatable/data.table

Similar issues

More R issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.