作为 this question 的跟进,我决定走 Rcpp 与 R 中复杂语法的路线。我认为这将提供更好的可读性(也可能更快)。
假设我有一个 data.frame
列表(我可以通过 as
轻松地将其转换为矩阵)。事先给出answe -r -s ,这似乎是最好的方法。
# input data
my_list <- vector("list", length= 10)
set.seed(65L)
for (i in 1:10) {
my_list[[i]] <- data.frame(matrix(rnorm(10000),ncol=10))
# alternatively
# my_list[[i]] <- matrix(rnorm(10000),ncol=10)
}
从矩阵中提取行的合适方法是什么?目标是创建一个列表,其中每个列表元素都包含每个原始列表的 data.frames 的第 nr
行的列表。我尝试了几种不同的语法并不断收到错误:
#include <Rcpp.h>
using namespace Rcpp;
using namespace std:
List foo(const List& my_list, const int& n_geo) {
int n_list = my_list.size();
std::vector<std::vector<double> > list2(n_geo);
// needed code....
return wrap(list2);
}
选项
for (int i = 0; i < n_list; i++) {
for (int nr = 0; nr < n_geo; nr++) {
list2[nr][i] = my_list[i].row(nr);
// or list2[nr].push_back(my_list[i].row(nr));
// or list2[nr].push_back(as<double>(my_list[i].row(nr)));
// or list2[nr].push_back(as<double>(my_list[i](nr, _)));
}
}
// or:
NumericMatrix a = my_list[1]
...
NumericMatrix j = my_list[10]
for (int nr = 0; nr < n_geo; nr++) {
list2[nr][1] = // as above
}
这些都不适合我。我究竟做错了什么?以下是我从上述语法选择中收到的错误。
error: no matching function for call to 'as(Rcpp::Matrix<14>::Row)'
或
error: cannot convert 'Rcpp::Matrix<14>::Row {aka Rcpp::MatrixRow<14>}' to 'double' in assignment
最佳答案
这是一种方法:
#include <Rcpp.h>
// x[[nx]][ny,] -> y[[ny]][[nx]]
// [[Rcpp::export]]
Rcpp::List Transform(Rcpp::List x) {
R_xlen_t nx = x.size(), ny = Rcpp::as<Rcpp::NumericMatrix>(x[0]).nrow();
Rcpp::List y(ny);
for (R_xlen_t iy = 0; iy < ny; iy++) {
Rcpp::List tmp(nx);
for (R_xlen_t ix = 0; ix < nx; ix++) {
Rcpp::NumericMatrix mtmp = Rcpp::as<Rcpp::NumericMatrix>(x[ix]);
tmp[ix] = mtmp.row(iy);
}
y[iy] = tmp;
}
return y;
}
/*** R
L1 <- lapply(1:10, function(x) {
matrix(rnorm(20), ncol = 5)
})
L2 <- lapply(1:nrow(L1[[1]]), function(x) {
lapply(L1, function(y) unlist(y[x,]))
})
all.equal(L2, Transform(L1))
#[1] TRUE
microbenchmark::microbenchmark(
"R" = lapply(1:nrow(L1[[1]]), function(x) {
lapply(L1, function(y) unlist(y[x,]))
}),
"Cpp" = Transform(L1),
times = 200L)
#Unit: microseconds
#expr min lq mean median uq max neval
# R 254.660 316.627 383.92739 347.547 392.7705 1909.097 200
#Cpp 18.314 26.007 71.58795 30.230 38.8650 945.167 200
*/
我不确定这将如何扩展;我认为这只是一种本质上效率低下的转型。根据我在源代码顶部的评论,您似乎只是在进行一种坐标交换——ny
nx
的第 1 行输入列表的第 th 个元素变为 nx
ny
的第一个元素输出列表的第 th 个元素:
x[[nx]][ny,] -> y[[ny]][[nx]]
要解决您遇到的错误,Rcpp::List
是一个通用对象——技术上是一个 Rcpp::Vector<VECSXP>
- 所以当你尝试做的时候,例如
my_list[i].row(nr)
编译器不知道 my_list[i]
是 NumericMatrix
.因此,您必须使用 Rcpp::as<>
进行显式转换,
Rcpp::NumericMatrix mtmp = Rcpp::as<Rcpp::NumericMatrix>(x[ix]);
tmp[ix] = mtmp.row(iy);
我刚刚使用了 matrix
示例数据中的元素来简化事情。在实践中,你最好不要强制 data.frame
s 至 matrix
对象直接在 R 中而不是在 C++ 中尝试;它会简单得多,而且很可能,强制转换只是调用底层 C 代码,因此尝试以其他方式执行它实际上没有任何好处。
我还应该指出,如果您使用的是 Rcpp::List
在同类类型中,您可以使用 Rcpp::ListOf<type>
获得更多性能.这将允许您跳过 Rcpp::as<type>
上面完成的转换:
typedef Rcpp::ListOf<Rcpp::NumericMatrix> MatList;
// [[Rcpp::export]]
Rcpp::List Transform2(MatList x) {
R_xlen_t nx = x.size(), ny = x[0].nrow();
Rcpp::List y(ny);
for (R_xlen_t iy = 0; iy < ny; iy++) {
Rcpp::List tmp(nx);
for (R_xlen_t ix = 0; ix < nx; ix++) {
tmp[ix] = x[ix].row(iy);
}
y[iy] = tmp;
}
return y;
}
/*** R
L1 <- lapply(1:10, function(x) {
matrix(rnorm(20000), ncol = 100)
})
L2 <- lapply(1:nrow(L1[[1]]), function(x) {
lapply(L1, function(y) unlist(y[x,]))
})
microbenchmark::microbenchmark(
"R" = lapply(1:nrow(L1[[1]]), function(x) {
lapply(L1, function(y) unlist(y[x,]))
}),
"Transform" = Transform(L1),
"Transform2" = Transform2(L1),
times = 200L)
#Unit: microseconds
# expr min lq mean median uq max neval
# R 6049.594 6318.822 7604.871 6707.242 8592.510 64005.190 200
# Transform 928.468 1041.936 3130.959 1166.819 1659.745 71552.284 200
#Transform2 850.912 957.918 1694.329 1061.183 2856.724 4502.065 200
*/
关于c++ - Rcpp - 从矩阵/数据帧列表中提取行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35996245/