From sfalcon at fhcrc.org  Fri Jun  8 08:41:26 2007
From: sfalcon at fhcrc.org (Seth Falcon)
Date: Thu, 07 Jun 2007 23:41:26 -0700
Subject: [Rd] missing IntegerFromString()
In-Reply-To: <6BA3828721B5434DAAE1E65C25715A6E1ABCA5@dph002.hpi-mcw.lan>
	(Aniko Szabo's message of "Thu, 7 Jun 2007 14:47:47 -0500")
References: <6BA3828721B5434DAAE1E65C25715A6E1ABCA5@dph002.hpi-mcw.lan>
Message-ID: <m2645z6j8p.fsf@ziti.local>

"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



[Rd] missing IntegerFromString()
Seth Falcon