Rcpp:我想要一个函数来读取DataFrame,为什么可以读取一个charactor向量并将其作为因子处理?

时间:2016-07-04 07:49:54

标签: c++ r types dataframe rcpp

这是一个最小的例子:

// Enable C++11 via this plugin
// [[Rcpp::plugins("cpp11")]]

#include <Rcpp.h>
#include <string>
using namespace Rcpp;
using namespace std;
/**
 * Wrapper function for testFunc
 */
// [[Rcpp::export]]
void testFunc(DataFrame vars_ ) {
  cout << "Type of vars_ " << TYPEOF(vars_) << endl;
  DataFrame vars = clone(vars_);
  CharacterVector out(vars_.nrows());
  vector<string>  varsV(vars_.nrows());
  vector<int> varsVInt(vars_.nrows());

  //Matrix is filled with input, Types are set to string
  for (int i = 0 ; i< vars_.size(); i++){

    if(TYPEOF(vars_[i])==STRSXP) {

      varsV=Rcpp::as<std::vector<string> > (vars_[i]);
      cout << "Type of the column " << i << " of the input data.frame is String."  << endl;

      for(int j=0; j<vars_.nrows(); j++){
        cout << "varsV["<<j<<"] "<<varsV[j] << endl;
      }

    }

    if(TYPEOF(vars_[i])==INTSXP) {

      cout << "Type of the column " << i << " of the input data.frame is Int/Factor."  << endl;
      varsVInt = Rcpp::as<std::vector<int> > (vars_[i]);

      if(varsVInt.size()>0)
      {

        for(int j=0; j<vars_.nrows(); j++){
          varsV[j]=to_string(varsVInt[j]);//Copy the vector to the string
          cout << "varsV["<<j<<"] "<<varsV[j] << endl;
        } // end for

      } //end if

    } // end if 

  } // endfor

}

执行代码:

ID <- c("0","1","2")
NAME2 <- c("Milla", "Milla","Miller")

dfString <-data.frame(ID,NAME2, stringsAsFactors=F)

################# test function

A = testFunc(as.data.frame(dfString[,2],stringsAsFactors=F))
B = testFunc(as.data.frame(dfString[,2]))

拨打:

A = testFunc(as.data.frame(dfString[,2],stringsAsFactors=F))

输出:

Type of vars_ 19
Type of the column 0 of the input data.frame is String.
varsV[0] Milla
varsV[1] Milla
varsV[2] Miller

拨打:

B = testFunc(as.data.frame(dfString[,2]))

输出:

Type of vars_ 19
Type of the column 0 of the input data.frame is Int/Factor.
varsV[0] 1
varsV[1] 1
varsV[2] 2

void testFunc(DataFrame vars_ )我希望有一个Rcpp::DataFrame,但是如果我粘贴一个data.frame的列,它是类character,那么它也可以在不抛出的情况下读取错误,因为类型似乎相同(类型19)。此character vector处理factor,请参阅输出。

有没有办法强制使用正确的类型(= Rcpp::DataFrame)?我不希望该功能的用户意外粘贴错误的类型。

0 个答案:

没有答案