As.integer score (max (factorize (15)))

It looks rather strange to me, and I would like an explanation. I

library(gmp) factorize(15) # => "3" "5" max(factorize(15)) # => "5" as.integer("5") # => 5 as.integer(max(factorize(15))) # => 1 0 0 0 1 0 0 0 1 0 0 0 5 0 0 0 

I can do what I want:

 max(as.numeric(factorize(15))) # => [1]5 

But it shocked me that I could not rely on nesting functions inside functions in a supposedly schematic language. Did I miss something?

+4
source share
3 answers

Well, the answer in factorize(15) view:

 > dput(factorize(15)) structure(c(02, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 03, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 05, 00, 00, 00), class = "bigz") 

and

 > dput(max(factorize(15))) structure(c(01, 00, 00, 00, 01, 00, 00, 00, 01, 00, 00, 00, 05, 00, 00, 00), class = "bigz") 

... max and as.numeric (actually, as.double ) have methods for the bigz class, but apparently as.integer does not execute:

 > methods(max) [1] max.bigq max.bigz > methods(as.numeric) no methods were found > methods(as.double) [1] as.double.bigq as.double.bigz as.double.difftime as.double.POSIXlt > methods(as.integer) no methods were found 

... therefore, as.integer treats as.integer objects as a simple vector of values.

+5
source

The result factorize in the gmp package is an object of the bigz class:

 > factorize(15) [1] "3" "5" > str(factorize(15)) Class 'bigz' raw [1:28] 02 00 00 00 ... 

From the help for ?biginteger it seems that the following coercion functions are defined for objects of the bigz class:

  • as.character
  • as.double

Please note that there is no as.integer in the list. So, to convert your result to numeric, you should use as.double :

 > as.double(max(factorize(15))) [1] 5 
+3
source

I agree that it seems strange that the 'bigz' class does not have an as.integer method. However, it may be that the whole point of the gmp package is to keep their values ​​“out of the hands” of less capable regular representation structures R. The authors have not decided (yet) to offer an integer coercion function, but you can always offer them what they should. As @Andrie demonstrated, such a function can be defined to intercept what otherwise would be the .Primitive call that you now receive with as.integer() .

 require(gmp) methods(as) 
+2
source

All Articles