Skip to content
Prev 25285 / 63424 Next

missing IntegerFromString()

On Thu, 7 Jun 2007, Douglas Bates wrote:

            
I have to say I am puzzled.  IntegerFromString works on a CHARSXP, and 
furthermore does not check its argument (which is one reason why it is no 
longer even exposed).  AFAICS, coerceVector(x,INTSXP) will not accept a 
CHARSXP, but it will accept a STRSXP.  (The same for asInteger.)

So two ways to do this for vector 'x' are

asInteger(x)
INTEGER(coerceVector(x,INTSXP))[0]

provided in the second case you know 'x' has length at least 1.  But the 
first is both safer and more efficient.

If you really have a CHARSXP, then you are not really supposed to work 
with 'bare' CHARSXPs unless you know what you are doing (in which case you 
don't need to ask ...).