rcpp-R会话已中止 库(Rcpp) 图书馆(geoR) levationd=as.matrix(data.frame(xcoords=elevation$coords[,1], ycoords=高程$coords[,2],elev=高程$data)) 提升 cppFunction('无效a(数值矩阵数据){ int nr=data.nrow(); int nc=data.ncol(); 数字向量tmp; 对于(inti;i

rcpp-R会话已中止 库(Rcpp) 图书馆(geoR) levationd=as.matrix(data.frame(xcoords=elevation$coords[,1], ycoords=高程$coords[,2],elev=高程$data)) 提升 cppFunction('无效a(数值矩阵数据){ int nr=data.nrow(); int nc=data.ncol(); 数字向量tmp; 对于(inti;i,r,rcpp,geor,R,Rcpp,Geor,Roland已经给了您一个很好的提示,您还有两个类型错误,并且在向量初始化时遗漏了一个大小 下面是一个修复版本,作为C++文件,可以用相关的R代码自动执行: cppFunction('NumericMatrix f1(NumericMatrix data){ int nr = data.nrow(); int nc = data.ncol(); NumericMatrix mat(nr, nr); for (int i=0; i<nr; i++){ for(int

Roland已经给了您一个很好的提示,您还有两个类型错误,并且在向量初始化时遗漏了一个大小

下面是一个修复版本,作为C++文件,可以用相关的R代码自动执行:

cppFunction('NumericMatrix f1(NumericMatrix data){
  int nr = data.nrow();
  int nc = data.ncol();
  NumericMatrix mat(nr, nr);
  for (int i=0; i<nr; i++){
    for(int j=0; j<nr; j++){
      mat(i,j) = (data(i,2)-data(j,2))*(data(i,2)-data(j,2));
    }
  }
  return mat;
}')
R>Rcpp::sourceCpp(“/tmp/jiwon.cpp”)
R> 图书馆(geoR)
--------------------------------------------------------------
地质统计数据分析
有关George的介绍,请访问http://www.leg.ufpr.br/geoR
geoR版本1.7-5.2(构建于2016-05-02)现已加载
--------------------------------------------------------------
R> 高程D str(高程D)
数值[1:52,1:3]0.31.42.43.65.71.62.93.43.44.8。。。
-属性(*,“dimnames”)=2个列表
..$:chr[1:52]“1”“2”“3”“4”。。。
..$:chr[1:3]“xcoords”“ycoords”“elev”
R> str(a(提升D))
数字[1:52,1:52]059291325324004900。。。
R>
下面是修复后的代码,解决了您的一些错误:

R> Rcpp::sourceCpp("/tmp/jiwon.cpp")

R> library(geoR)
--------------------------------------------------------------
 Analysis of Geostatistical Data
 For an Introduction to geoR go to http://www.leg.ufpr.br/geoR
 geoR version 1.7-5.2 (built on 2016-05-02) is now loaded
--------------------------------------------------------------


R> elevationd <- as.matrix(data.frame(xcoords=elevation$coords[,1], 
+                                    ycoords=elevation$coords[,2], 
+             .... [TRUNCATED] 

R> str(elevationd)
 num [1:52, 1:3] 0.3 1.4 2.4 3.6 5.7 1.6 2.9 3.4 3.4 4.8 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:52] "1" "2" "3" "4" ...
  ..$ : chr [1:3] "xcoords" "ycoords" "elev"

R> str(a(elevationd))
 num [1:52, 1:52] 0 5929 13225 32400 4900 ...
R>
#包括
使用名称空间Rcpp;
//[[Rcpp::导出]]
数字矩阵a(数字矩阵数据){
int nr=data.nrow();
int nc=data.ncol();
数字向量tmp(nr);

对于(int i=0;ILooCo)关于C++中的循环的一些教程。需要初始化I为一个值。
R> Rcpp::sourceCpp("/tmp/jiwon.cpp")

R> library(geoR)
--------------------------------------------------------------
 Analysis of Geostatistical Data
 For an Introduction to geoR go to http://www.leg.ufpr.br/geoR
 geoR version 1.7-5.2 (built on 2016-05-02) is now loaded
--------------------------------------------------------------


R> elevationd <- as.matrix(data.frame(xcoords=elevation$coords[,1], 
+                                    ycoords=elevation$coords[,2], 
+             .... [TRUNCATED] 

R> str(elevationd)
 num [1:52, 1:3] 0.3 1.4 2.4 3.6 5.7 1.6 2.9 3.4 3.4 4.8 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:52] "1" "2" "3" "4" ...
  ..$ : chr [1:3] "xcoords" "ycoords" "elev"

R> str(a(elevationd))
 num [1:52, 1:52] 0 5929 13225 32400 4900 ...
R>
#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
NumericMatrix a(NumericMatrix data){
  int nr = data.nrow();
  int nc = data.ncol();
  NumericVector tmp(nr);
  for (int i=0; i<nr; i++){
    tmp[i] = data(i,2);
  }
  NumericMatrix mat(nr, nr);
  for (int i=0; i<nr; i++){
    for (int j=0; j<nr; j++){
      mat(i,j) = (tmp[i] - tmp[j])*(tmp[i] - tmp[j]);
    }
  }
  return mat;
}

/*** R
library(geoR)
elevationd <- as.matrix(data.frame(xcoords=elevation$coords[,1], 
                                   ycoords=elevation$coords[,2], 
                                   elev=elevation$data))
str(elevationd)
str(a(elevationd))
*/