英文:
data.tables with Rcpp (datatableAPI.h)
问题
R
name <- c("a", "a", "a", "a", "b")
value <- c(100, 200, 300, 400, 500)
group <- c("aa", "ab", "aa", "ab", "ab")
data <- data.table(name, value, group)
count <- 3
for(i in 1:count){
data3 <- data[name == "a", .(mean_value = mean(value)), by = group]
}
data3
Rcpp
library(data.table)
Rcpp::cppFunction("SEXP analysis(SEXP data) { return dt::subsetDT(data[name == 'a', .(mean_value = mean(value)), by = group]); }",
include="#include <datatableAPI.h>",
depends="data.table")
name <- c("a", "a", "a", "a", "b")
value <- c(100, 200, 300, 400, 500)
group <- c("aa", "ab", "aa", "ab", "ab")
data <- data.table(name, value, group)
analysis(data)
英文:
I am new to Rcpp and learn to use Rcpp with data.table package in order to speed up the R code performance in R Markdown as I want to generate report. From https://github.com/Rdatatable/data.table/issues/4643, there is datatableAPI.h but I am not able to apply my own analysis to Rcpp with data.table. It produces errors as I tried to amend the following R code to suit my own analysis as I have tried a lot methods. I am able to run the following RCpp codes from https://github.com/Rdatatable/data.table/issues/4643:
RCpp
library(data.table)
dt <- data.table(iris)
Rcpp::cppFunction("SEXP mysub2(SEXP x, SEXP rows, SEXP cols) { return dt::subsetDT(x,rows,cols); }",
include="#include <datatableAPI.h>",
depends="data.table")
mysub2(dt, 1:4, 1:4)
May I know how to transfer the R code below to Rccp code as shown above? Your help is appreciated.
R
name <- c("a", "a", "a", "a", "b")
value <- c(100, 200, 300, 400, 500)
group <- c("aa", "ab", "aa", "ab", "ab")
data <- data.table(name, value, group)
count <- 3
for(i in 1: count){
data3 <- data[name == "a", .(mean_value = mean(value)), by = group]
}
data3
The Rcpp code below is my current work. It shows error "
Error in sourceCpp(code = code, env = env, rebuild = rebuild, cacheDir = cacheDir, :
Error 1 occurred building shared library."
library(data.table)
Rcpp::cppFunction("SEXP analysis(SEXP data) { return dt::subsetDT(data[name == 'a', .(mean_value = mean(value)), by = group]); }",
include="#include <datatableAPI.h>",
depends="data.table")
name <- c("a", "a", "a", "a", "b")
value <- c(100, 200, 300, 400, 500)
group <- c("aa", "ab", "aa", "ab", "ab")
data <- data.table(name, value, group)
analysis(data)
Thank you.
答案1
得分: 2
欢迎来到 StackOverflow!我很高兴你找到了 data.table
的标题并使其工作 - 初步的步骤应该很容易!
但我们需要看看它提供了什么:
/* 为 C++ 使用添加一个命名空间 */
namespace dt {
inline SEXP subsetDT(SEXP x, SEXP rows, SEXP cols) {
return DT_subsetDT(x, rows, cols); }
}
按行和列进行索引,就像你在示例中所做的一样。
现在看看你的示例:它并不匹配。你假设你可以像在 R 提示符中那样提交任何奇怪的 data.table
表达式。编译器告诉你你不能这样做。
可惜,编译器是对的。这基本上是我们在这里可以说的所有内容。
英文:
Welcome to StackOverflow! I am glad you found the header for data.table
and got it work -- first steps are supposed to be easy!
But we need to look at what it offers:
/* add a namespace for C++ use */
namespace dt {
inline SEXP subsetDT(SEXP x, SEXP rows, SEXP cols) {
return DT_subsetDT(x, rows, cols); }
}
Indexing by rows and columns. As you did in the example.
Now look at your example: it does not match. You assume you can submit any odd data.table
expression as you do at the R prompt. The compiler tells you that you cannot.
Sadly, the compiler is right. And that is really all we can say here.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论