RobinHankin / frab

1 stars 0 forks source link

missing replacement methods #7

Open RobinHankin opened 1 year ago

RobinHankin commented 1 year ago

It seems to be quite difficult to replace values of a frab object. Given a frab with some negative values, it is quite easy to replace the negative values with a particular scalar:

library("frab")
set.seed(0)
(x <- rfrab(10,-2:2))
#> A frab object with entries
#>  c  d  e  g  i 
#> -2 -1  2  1  2
x[x<0] <- 0
x
#> A frab object with entries
#> e g i 
#> 2 1 2

However, something like abs(x) seems to be much harder:

library("frab")
set.seed(0)
(x <- rfrab(10,-2:2))
#> A frab object with entries
#>  c  d  e  g  i 
#> -2 -1  2  1  2
abs(x)
#> Error in abs(x): non-numeric argument to mathematical function
x[x<0]
#> A frab object with entries
#>  c  d 
#> -2 -1
x[x<0] <- -x[x<0]
#> Error in .local(x, i, j, ..., value): replacement operation not defined in this case

Above, x[x<0] <- -x[x<0] would seem to have a very reasonable expectation, but it does not work, due to a lack of

setReplaceMethod("[",signature(x="frab",i="disord",j="missing",value="frab"),...)

which would seem to be relatively straightforward to implement. Actually the best way I have found in existing package idiom to achieve abs(x) is this:

suppressMessages(library("frab"))
set.seed(0)
(x <- rfrab(10,-2:2))
#> A frab object with entries
#>  c  d  e  g  i 
#> -2 -1  2  1  2
frab(abs(as.namedvector(x)))
#> A frab object with entries
#> c d e g i 
#> 2 1 2 1 2

Part of the problem is that we do not have a values<-() method. But I need to make values() and values<-() in the disordR package generic first.

RobinHankin commented 1 year ago

One major problem is that the hash code of frabs with extractions defined by disords have a changed hash key:

suppressMessages(library("frab"))
set.seed(0)
options(frab_print_hash = TRUE)
x <- rfrab(10,-2:2)
x
#> A frab object with hash a6276102b30efd3240679a420b8ae6447ec0538a and entries
#>  c  d  e  g  i 
#> -2 -1  2  1  2
x[x<0]
#> A frab object with hash 7ce6e486250d5c2c05f3182aecefab571c377881 and entries
#>  c  d 
#> -2 -1

Above we see that the hash key of x and x[x<0] are different, and arguably they should match.