我用 C++ 编写了这个函数:
extern "C"
{
void add(int* first, int* second, int *n , int* sum)
{
for (int i = 0; i< *n; i++)
{
sum[i] = first[i] + second[i];
}
}
}
和这个司机:
add <- function(twoColumn)
{
if(!is.data.frame(twoColumn))stop("twoColumn should be data.frame")
first <- twoColumn[1]
second <- twoColumn[2]
n <- length(first)
.C("add",first = as.integer(unlist(first)),second = as.integer(unlist(second)), n = as.integer(n),sum = as.integer(rep(0,n)))$sum
}
但 R 输出只是数据帧第一行总和的一个数字。