r - 如何使用.C接口将数组返回给R
问题描述
我有这个简单的代码:
void test(int *testvec, int *testlen, int *testres) {
for (int i = 0; i < *testlen; i++){
testres[i]=0;
if (testvec[i] < 0){
testres[i] = -1;}
else if (testvec[i] == 0){
testres[i] = 0;}
else {testres[i] = 1;}
}
}
}
当我经过
testresult = function (testvec, len){
signC = .C("signC", as.integer(testvec), as.integer(length(testvec)), as.integer(testvector("integer"), length(testvec)))
return (signC)
}
testvec = c(1,2,-3,0,2)
似乎什么都没有处理。我对 C 很陌生,我不明白如何在 R 中的 .C 接口的竞赛中动态地为数组赋值。
谢谢您的帮助。
解决方案
.C
接口函数不能返回值。如果要修改 C 中的值,则需要将足够大小的向量传递给函数,并写入该向量:
sign = function (testvec) {
sign_c = integer(length(testvec))
.C("signC", as.integer(testvec), length(testvec), sign_c)
sign_c
}
推荐阅读
- python - 如何将 csv 文件读取到 numpy ndarrays
- python - 使用 Python 和 Flask 在网络上更新图像
- r - 关闭 saveGIF 显示 RStudio
- sql-server - 将 XML 导入 SQL Server DB - 得到空白结果
- linux - 如何通过用户权限在 Linux debian 中隐藏 Python 代码?
- javascript - 将一些初始数据传递到新窗口的正确方法是什么?
- python - 通过 for 循环用用户输入填充列表
- r - 为什么训练数据上的线性回归拟合值和预测值存在差异?
- firebase-authentication - 托管公司托管的域上的 Firebase Web 登录?
- vue.js - 如何对 vue 中的变量进行单元测试并用 jest 进行 vuetify?