[Rcpp-devel] How get colnames and rownames in Rcpp method?

Romain Francois romain at r-enthusiasts.com
Thu Jul 29 09:12:50 CEST 2010


Le 29/07/10 03:58, Young-Ju Park a écrit :
> Hi all,
> I have a question.
> How get colnames and rownames in Rcpp method?
> attecthed file : RGui.exe capture
> my work environment :
> R version : 2.11.1
> Development Language : VC++
> OS : WinXP Pro sp3
> Thanks and best regards.
> Young-Ju, Park
> from Korea

Hi,

This is not a reproducible example, the screenshot is very irrelevant to 
the question I think you are asking. I'll reply to what I think you are 
asking which is "how to get the colnames of a matrix in Rcpp".

colnames and rownames in R are stored in the "dimnames" attribute of 
matrices, so in Rcpp you can access them using the attr member function:

require( inline )
m <- outer(
	c( A = 4, B = 5 ),
	c( C = 4, D = 5 )
	, "+" )
f <- cxxfunction( signature( x_ = "matrix" ), '
    NumericMatrix x(x_) ;
    List dimnames = x.attr( "dimnames" ) ;
    return dimnames[0] ;
', plugin = "Rcpp" )
f( m )



Another way is to invoke the rownames function from inside C++.

f <- cxxfunction( signature( x_ = "matrix" ), '
    NumericMatrix x(x_) ;
    Function rownames("rownames") ;
    return rownames(x) ;
', plugin = "Rcpp" )
f( m )

Does that answer your question ?

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/aryfrk : useR! 2010
|- http://bit.ly/bc8jNi : Rcpp 0.8.4
`- http://bit.ly/dz0RlX : bibtex 0.2-1



More information about the Rcpp-devel mailing list