[Rcpp-devel] indexing matrix by row names and column names
Dirk Eddelbuettel
edd at debian.org
Wed Mar 2 18:06:35 CET 2011
On 2 March 2011 at 12:18, Mahdi Shafiei wrote:
| Hello,
| In R, one can define a matrix and assign names to tows and columns:
|
| A = matrix(1:9, nrow=3, ncol=3);
| rownames(A) = c("R1", "R2", "R3")
| colnames(A) = c("C1", "C2", "C3")
|
| and then access entries in A by these row and column names instead of row and
| column numbers like:
|
| A["R1", "C1"]
|
| I tried to find out if this is possible in some way in Rcpp for Matrix type. I
| know that one can set rownames and colnames attributes for an object of type
| Matrix but my understanding is that this is done for preparing an object to be
| passed to R.
|
| Is there a way to retrieve entries of a let say NumericMatrix by row and column
| names ?
You can certainly created named vectors, lists, ... etc so that they arrive
in R the proper way. And you certainly index list elements by name as we do
that all the time for parameter values passed in, eg in RQuantLib I do
RcppExport SEXP EuropeanOption(SEXP optionParameters) {
try {
Rcpp::List rparam(optionParameters); // Parameters via R list object
std::string type = Rcpp::as<std::string>(rparam["type"]);
double underlying = Rcpp::as<double>(rparam["underlying"]);
double strike = Rcpp::as<double>(rparam["strike"]);
QuantLib::Spread dividendYield = Rcpp::as<double>(rparam["dividendYield"]);
QuantLib::Rate riskFreeRate = Rcpp::as<double>(rparam["riskFreeRate"]);
QuantLib::Time maturity = Rcpp::as<double>(rparam["maturity"]);
int length = int(maturity*360 + 0.5); // FIXME: this could be better
double volatility = Rcpp::as<double>(rparam["volatility"]);
and there are more examples in the other packages using Rcpp.
Now, for NumericMatrix and friends: dunno. When I have a question like this I
start by looking (or grep'ing) around the unitTests directory...
Dirk
|
|
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
More information about the Rcpp-devel
mailing list