[Rd] missing IntegerFromString()

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Jun 8 08:34:00 CEST 2007

On Thu, 7 Jun 2007, Aniko Szabo wrote:

> I am sorry about the confusion, I was too hasty.
> asInteger(coerceVector(x,INTSXP)) does not work after all. Here are more
> details of what I am trying to accomplish: I have a matrix with column
> names that are actually known to be integers (because I set them so
> myself in the R code, say, colnames(mat) <- 1:10. Of course, they become
> converted to character strings.)
> The relevant part of my code used to be:
> SEXP MyFunction(SEXP mat);
> 	int warn, minY
> 	SEXP rl, cl;
> 	char *rn, *cn;
> 	GetMatrixDimnames(mat, &rl, &cl, &rn, &cn);
> 	minY = IntegerFromString(VECTOR_ELT(cl,0), &warn);
> 	if (warn > 0) error("Names of popmatrix columns are not
> integers");
> Running some tests it appears that VECTOR_ELT(cl,0) is CHARSXP (which I
> wound up using without even knowing it).
> I tried replacing the IntegerFromString part with both
> asInteger(VECTOR_ELT(cl,0)) and with
> asInteger(coerceVector(VECTOR_ELT(cl,0),INTSXP)), but as you surmised,
> since VECTOR_ELT(cl,0) is CHARSXP, it does not work.
> So, how could I get the actual values in the column names?

 	if(length(cl) > 0)
 	    minY = INTEGER(coerceVector(cl, INTSXP))[0];
 	else minY = NA_INTEGER;

You are assuming that there is at least one dimname, and your code needs 
more careful checks.  cl could be NULL and it could be of length 0.  If 
not NULL it is (currently) guaranteed to be a STRSXP.

In future versions of R 'minY = asInteger(cl)' will be all that is needed.

[Private message posted to the list deleted: you are not allowed to do 
that, as the posting guide points out.  It is a breach of copyright law.]

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-devel mailing list