如何在Rcpp中获取整数rownames?

时间:2015-11-09 20:35:41

标签: c++ r 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等)中的rownames作为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:在函数&#39; Rcpp :: IntegerVector rownames1(Rcpp :: NumericMatrix)&#39;:   file1b9c6dec3c12.cpp:11:40:错误:从类型&#39; Rcpp :: Vector&lt; 16&gt; :: Proxy {aka Rcpp :: internal :: string_proxy&lt; 16&gt;}&#39;输入&#39; int&#39;   make:*** [file1b9c6dec3c12.o]错误1   警告信息:   run command&#39; make -f&#34; C:/PROGRA~1/R/R-32~1.2/etc/x64/Makeconf" -f&#34; C:/PROGRA~1/R/R-32~1.2/share/make/winshlib.mk" SHLIB_LDFLAGS =&#39; $(SHLIB_CXXLDFLAGS)&#39; SHLIB_LD =&#39; $(SHLIB_CXXLD)&#39; SHLIB =&#34; sourceCpp_23.dll&#34; WIN = 64 TCLBIN = 64 OBJECTS =&#34; file1b9c6dec3c12.o&#34;&#39;有状态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:在函数&#39; Rcpp :: IntegerVector rownames1(Rcpp :: NumericMatrix)&#39;:   file1b9c71d25b92.cpp:11:38:错误:&#39;运算符的模糊重载 - &#39; in&#39; Rcpp :: Vector :: operator [with int RTYPE = 16,StoragePolicy = Rcpp :: PreserveStorage,Rcpp :: Vector :: Proxy = Rcpp :: internal :: string_proxy&lt; 16&gt ;, R_xlen_t = long long int ](((long long int)i)) - &#34; 0&#34;&#39;   file1b9c71d25b92.cpp:11:38:注意:候选人是:   file1b9c71d25b92.cpp:11:38:注意:operator-(const char *,const char *)   file1b9c71d25b92.cpp:11:38:注意:operator-(const char *,const char *)   file1b9c71d25b92.cpp:11:38:注意:operator-(char *,char *)   make:*** [file1b9c71d25b92.o]错误1   警告信息:   run command&#39; make -f&#34; C:/PROGRA~1/R/R-32~1.2/etc/x64/Makeconf" -f&#34; C:/PROGRA~1/R/R-32~1.2/share/make/winshlib.mk" SHLIB_LDFLAGS =&#39; $(SHLIB_CXXLDFLAGS)&#39; SHLIB_LD =&#39; $(SHLIB_CXXLD)&#39; SHLIB =&#34; sourceCpp_21.dll&#34; WIN = 64 TCLBIN = 64 OBJECTS =&#34; file1b9c71d25b92.o&#34;&#39;有状态2

任何帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

您可以使用cstdlib函数atoiconst 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

*/