Re: [Rd] missing IntegerFromString()

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Fri, 08 Jun 2007 07:34:00 +0100 (BST)

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

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Fri 08 Jun 2007 - 06:38:41 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 08 Jun 2007 - 13:34:12 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.