When two tidytables are subtracted, the class `tidytable` gets lost on the way.
jospueyo opened this issue · 2 comments
I know probably this is an edge case. This is the first time I need to subtract to dataframes as if they were matrix, but I think it's also easy to fix just adding a method to -
for tidytable, isn't it?
library(tidytable)
#>
#> Attaching package: 'tidytable'
#> The following objects are masked from 'package:stats':
#>
#> dt, filter, lag
#> The following object is masked from 'package:base':
#>
#> %in%
t1 <- tidytable(a = rnorm(5), b = rnorm(5))
t2 <- tidytable(c = runif(5), d = runif(5))
t1 - t2
#> a b
#> 1: -1.047649401 -1.9281914
#> 2: 0.914120675 -2.3204989
#> 3: -2.072748850 -1.3040519
#> 4: -0.465522532 -0.6819902
#> 5: -0.004895616 -0.1058110
class(t1)
#> [1] "tidytable" "data.table" "data.frame"
class(t2)
#> [1] "tidytable" "data.table" "data.frame"
class(t1 - t2)
#> [1] "data.table" "data.frame"
Created on 2024-01-19 with reprex v2.1.0
Hmm it doesn't look like data.table
ever defines a -.data.table
method. So not sure how it retains the data.table class.
If I do this on a tibble it strips all the subclasses and just leaves a data.frame.
library(dplyr)
t1 <- tibble(a = rnorm(5), b = rnorm(5))
t2 <- tibble(c = runif(5), d = runif(5))
t1 - t2
#> a b
#> 1 0.43871653 -0.7512329
#> 2 -1.28601158 -1.9205421
#> 3 -0.32871328 1.5004114
#> 4 0.05049482 0.5139137
#> 5 0.45431175 -0.7676874
class(t1 - t2)
#> [1] "data.frame"
I'll look into this, but I would rather not define tidytable methods for all mathematical operators. Especially since it doesn't appear to work in dplyr
either.
I think I'm going to close this one. Since it doesn't work with tibbles, I'm ok with it not working in tidytable either.