[Rd] missing IntegerFromString()

Seth Falcon sfalcon at fhcrc.org
Fri Jun 8 08:41:26 CEST 2007


"Aniko Szabo" <aszabo at hpi.mcw.edu> writes:

> 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?

How about:

  SEXP colnums;
  int *ivals;
  PROTECT(colnums = coerceVector(cl, INTSXP));
  ivals = INTEGER(colnums);

Here you convert the STRSXP cl into an INTSXP.  If you want the actual
integer values, use the ivals pointer.

+ seth

-- 
Seth Falcon | Computational Biology | Fred Hutchinson Cancer Research Center
http://bioconductor.org



More information about the R-devel mailing list