Working off of these two posts
1. Convert char to int in C and C++
2. http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2010-July/000932.html
I'm trying to get the rownames from an Rcpp
Matrix (NumericMatrix, etc) as an IntegerVector.
In R, this would be:
as.integer(rownames(x)) # where x is a matrix
I've tried casting in two different ways and am getting different compilation errors:
attempt 1
cppFunction('IntegerVector rownames1(NumericMatrix x) {
List dimnames = x.attr("dimnames");
CharacterVector rownames = dimnames[0];
IntegerVector out(dimnames.size());
for (int i= 0; i < out.size(); i++) {
out[i] = (int) rownames[i]; // cast via (int)
}
return (IntegerVector) dimnames[0];}')
file1b9c6dec3c12.cpp: In function 'Rcpp::IntegerVector rownames1(Rcpp::NumericMatrix)': file1b9c6dec3c12.cpp:11:40: error: invalid cast from type 'Rcpp::Vector<16>::Proxy {aka Rcpp::internal::string_proxy<16>}' to type 'int' make: *** [file1b9c6dec3c12.o] Error 1 Warning message: running command 'make -f "C:/PROGRA~1/R/R-32~1.2/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-32~1.2/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="sourceCpp_23.dll" WIN=64 TCLBIN=64 OBJECTS="file1b9c6dec3c12.o"' had status 2
attempt 2
cppFunction('IntegerVector rownames1(NumericMatrix x) {
List dimnames = x.attr("dimnames");
CharacterVector rownames = dimnames[0];
IntegerVector out(dimnames.size());
for (int i= 0; i < out.size(); i++) {
out[i] = rownames[i] + "0"; // cast as suggested in SO post linked above
}
return (IntegerVector) dimnames[0];}')
file1b9c71d25b92.cpp: In function 'Rcpp::IntegerVector rownames1(Rcpp::NumericMatrix)': file1b9c71d25b92.cpp:11:38: error: ambiguous overload for 'operator-' in 'Rcpp::Vector::operator [with int RTYPE = 16, StoragePolicy = Rcpp::PreserveStorage, Rcpp::Vector::Proxy = Rcpp::internal::string_proxy<16>, R_xlen_t = long long int](((long long int)i)) - "0"' file1b9c71d25b92.cpp:11:38: note: candidates are: file1b9c71d25b92.cpp:11:38: note: operator-(const char*, const char*) file1b9c71d25b92.cpp:11:38: note: operator-(const char*, const char*) file1b9c71d25b92.cpp:11:38: note: operator-(char*, char*) make: *** [file1b9c71d25b92.o] Error 1 Warning message: running command 'make -f "C:/PROGRA~1/R/R-32~1.2/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-32~1.2/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="sourceCpp_21.dll" WIN=64 TCLBIN=64 OBJECTS="file1b9c71d25b92.o"' had status 2
Any help appreciated!
coerceVector(rownames, INTSXP)
? – alexis_lazcoerceVector
in? I'm getting not-in-scope / not in namespace Rcpp errors – Alex WcoerceVector
is, kind of, the C equivalent ofas(..)
in R's API. Sorry, though, I'm not familiar with why you get these errors in Rcpp. – alexis_laz