简体   繁体   English

如何在Rcpp中获取整数行名?

[英]How to get integer rownames in Rcpp?

Working off of these two posts 在这两个职位上工作
1. Convert char to int in C and C++ 1. 在C和C ++中将char转换为int
2. http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2010-July/000932.html 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. 我正在尝试从Rcpp矩阵(NumericMatrix等)中获取行名作为IntegerVector。

In R, this would be: 在R中,这将是:

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 尝试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 file1b9c6dec3c12.cpp:在函数'Rcpp :: IntegerVector rownames1(Rcpp :: NumericMatrix)':file1b9c6dec3c12.cpp:11:40:错误:类型'Rcpp :: Vector <16> :: Proxy {aka Rcpp ::: internal :: string_proxy <16>}'键入'int'make:*** [file1b9c6dec3c12.o]错误1警告消息:运行命令'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”'状态为2

attempt 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 file1b9c71d25b92.cpp:在函数'Rcpp :: IntegerVector rownames1(Rcpp :: NumericMatrix)'中:file1b9c71d25b92.cpp:11:38:错误:'Rcpp :: Vector :: operator中'operator-'的重载[带有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:注意:候选对象是:file1b9c71d25b92.cpp:11:38:注意:operator-(const char *,const char *)file1b9c71d25b92.cpp:11:38:note:operator-(const char * ,const char *)file1b9c71d25b92.cpp:11:38:注意:operator-(char *,char *)make:*** [file1b9c71d25b92.o]错误1警告消息:运行命令'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”'的状态为2

Any help appreciated! 任何帮助表示赞赏!

You can use the cstdlib function atoi to convert from const char* to int . 您可以使用cstdlib函数atoiconst char*转换为int For example, 例如,

#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

*/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM