我想在包含列表列的Rcpp函数中生成数据帧。我已经尝试了好几件事,却找不到有效的解决方案。以下是我尝试过的Rcpp c ++文件:
#include <Rcpp.h> #include <vector> using namespace Rcpp; using namespace std; // [[Rcpp::export]] DataFrame makeListColumn() { vector<RawVector> the_future_list; the_future_list.push_back(RawVector::create(0, 1, 2)); the_future_list.push_back(RawVector::create(3, 4)); the_future_list.push_back(RawVector::create(5, 6, 7, 8, 9, 10)); vector<int> another_regular_column; another_regular_column.push_back(42); another_regular_column.push_back(24); another_regular_column.push_back(4242); DataFrame ret = DataFrame::create(Named("another_regular_column") = another_regular_column, Named("thelistcol") = the_future_list); return ret; } /*** R a = makeListColumn() dput(a) */
此输出为以下内容:
a = makeListColumn()
structure(list(another_regular_column = c(42L,24L,4242L,42L,24L,4242L),thelistcol.as.raw.c.0x00..0x01..0x02 .. = as.raw(c(0x00,0x01、0x02、0x00、0x01、0x02)),listcol.as.raw.c.0x03..0x04 .. = as.raw(c(0x03,0x04、0x03、0x04、0x03、0x04)),listcol.as.raw.c.0x05..0x06..0x07..0x08..0x09..0x0a .. = as.raw(c(0x05,0x06、0x07、0x08、0x09、0x0a))),类=“ data.frame”,row.names = c(NA,-6L))
我正在寻找以下内容(在常规R脚本中完成):
what_i_wanted = data.frame( another_regular_column = c(42, 24, 4242), thelistcol = I(list(as.raw(c(0,1,2)), as.raw(c(3, 4)), as.raw(c(5, 6, 7, 8, 9, 10)))) )
这将产生输出:
structure(list(another_regular_column = c(42,24,4242),thelistcol = structure(list(as.raw(c(0x00,0x01,0x02)),as.raw(c(0x03,0x04)),as.raw(c(0x05,0x06、0x07、0x08、0x09、0x0a))),类=“ AsIs”)),类=“ data.frame”,row.names = c(NA,-3L))
R和Rcpp方法之间的主要区别是R代码中的
I()
调用。如果删除该代码,则R代码生成的结构与Rcpp代码相同。我在Rcpp文档中做了一些查找,并做了一些google搜索,但是空手而归。
有人可以帮助我了解我需要在Rcpp中做什么才能使其正常工作?
编辑:
我确实尝试做类似的事情:
List the_list = List::create(the_future_list); the_list.attr("class") = CharacterVector::create("AsIs");
不幸的是,导致以下错误:
makeListColumn()中的错误:无法使用R函数转换:as.data.frame。
我想在包含列表列的Rcpp函数中生成数据帧。我已经尝试了好几件事,却找不到有效的解决方案。以下是Rcpp c ++文件...
AsIs
未实现。