Skip to content

forward operations to vectorised attributes #2948

@Enchufa2

Description

@Enchufa2

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    non-atomic columne.g. list columns, S4 vector columns

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions