[R] Accessing row and col names of SEXP objects
Prof Brian Ripley
ripley at stats.ox.ac.uk
Wed Dec 17 20:42:47 CET 2003
>From the file defining GetColNames:
/* "GetRowNames" and "GetColNames" are utility routines which
* locate and return the row names and column names from the
* dimnames attribute of a matrix. They are useful because
You have not applied them to the dimnames attribute. Extracting
dimnames attributes _is_ covered in R-exts:
SEXP dimnames=getAttrib(ts, R_DimNamesSymbol),
colnames = VECTOR_ELT(dimnames, 1);
for (j = 0; j < length(colnames); j++)
printf("%s\n",CHAR(STRING_ELT(colnames, j)));
looks about right, although you should do something if the colnames or
dimnames are NULL.
On Wed, 17 Dec 2003, Whit Armstrong wrote:
> Can someone lend me a hand with extracting the dimnames from a SEXP? I've
> looked through R-exts, but I couldn't find an example.
>
> Here is the code I'm using to grab the jth column name and print it, but the
> colnames I'm getting are garbage.
>
> None of the following are working.
>
> void printInfo(SEXP ts) {
>
> int j;
>
> for (j=0; j<col; j++) {
> printf("%s\n",CHAR(STRING_ELT(GetColNames(ts), j)));
> printf("%s\n",CHAR(VECTOR_ELT(GetColNames(ts), j)));
> printf("%s\n",CHARACTER_DATA(STRING_ELT(GetColNames(ts), j)));
> printf("%s\n",CHARACTER_DATA(VECTOR_ELT(GetColNames(ts), j)));
> }
>
> }
>
> Here is the object I'm passing in:
>
> tmp <- matrix(rnorm(100),ncol=5)
> colnames(tmp) <- c("tmp","a","b","c","d")
> rownames(tmp) <- 1:100
> .Call("printInfo",tmp)
>
> Thanks for your help.
>
> Regards,
> Whit
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
>
--
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-help
mailing list