New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: forward operations to vectorised attributes #2948

Open
Enchufa2 opened this Issue Jun 21, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@Enchufa2

Enchufa2 commented Jun 21, 2018

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
@MichaelChirico

This comment has been minimized.

Contributor

MichaelChirico commented Jun 22, 2018

@Enchufa2

This comment has been minimized.

Enchufa2 commented Jun 22, 2018

There's no additional output with verbose=TRUE.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment