如何在 Rcpp 中获取整数行名?
How to get integer rownames in Rcpp?
处理这两个帖子
1. Convert char to int in C and C++
2. http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2010-July/000932.html
我正在尝试从 Rcpp
矩阵(NumericMatrix 等)中获取行名作为 IntegerVector。
在 R 中,这将是:
as.integer(rownames(x)) # where x is a matrix
我尝试了两种不同的转换方式,但遇到了不同的编译错误:
尝试 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
尝试 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
感谢任何帮助!
您可以使用 cstdlib
函数 atoi
将 const char*
转换为 int
。例如,
#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::IntegerVector rownames1(Rcpp::NumericMatrix x) {
Rcpp::List dimnames = x.attr("dimnames");
Rcpp::CharacterVector rownames = dimnames[0];
Rcpp::IntegerVector out(rownames.size());
std::transform(rownames.begin(), rownames.end(), out.begin(), std::atoi);
return out;
}
/*** R
M <- matrix(
1.5:16.5, nrow = 4,
dimnames = list(1:4, 1:4))
##
R> all.equal(rownames1(M), as.integer(row.names(M)))
#[1] TRUE
*/
处理这两个帖子
1. Convert char to int in C and C++
2. http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2010-July/000932.html
我正在尝试从 Rcpp
矩阵(NumericMatrix 等)中获取行名作为 IntegerVector。
在 R 中,这将是:
as.integer(rownames(x)) # where x is a matrix
我尝试了两种不同的转换方式,但遇到了不同的编译错误:
尝试 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
尝试 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
感谢任何帮助!
您可以使用 cstdlib
函数 atoi
将 const char*
转换为 int
。例如,
#include <Rcpp.h>
// [[Rcpp::export]]
Rcpp::IntegerVector rownames1(Rcpp::NumericMatrix x) {
Rcpp::List dimnames = x.attr("dimnames");
Rcpp::CharacterVector rownames = dimnames[0];
Rcpp::IntegerVector out(rownames.size());
std::transform(rownames.begin(), rownames.end(), out.begin(), std::atoi);
return out;
}
/*** R
M <- matrix(
1.5:16.5, nrow = 4,
dimnames = list(1:4, 1:4))
##
R> all.equal(rownames1(M), as.integer(row.names(M)))
#[1] TRUE
*/