小编use*_*124的帖子

如何处理R到Rcpp中的列表

我在R中有一个列表x <-list(c(1,2,3),c(4,5),c(5,5),c(6)).我想将列表输入到Rcpp并将它们作为平均向量返回,c(2,4,5,5,6).

我不知道如何处理Rcpp中的列表.我收到了错误消息,有人可以查看我的代码吗?

library(inline)

fx = cxxfunction(signature(x='List'), body = 
'
    Rcpp::List xlist(x);
    int n = xlist.size();
    double res[n];

    for(int i=0; i<n; i++) {
        Rcpp NumericVector y(xlist[i]);
        int m=y.size();
        res[i]=0;
        for(int j=0; j<m; j++){
            res[i]=res[i]+y[j]  
        }
    }

  return(wrap(res));
'
, plugin='Rcpp')

x<-list(c(1,2,3), c(4,5), c(5,5), c(6))
fx(x)
Run Code Online (Sandbox Code Playgroud)

c++ r list rcpp

12
推荐指数
1
解决办法
8890
查看次数

双循环线性模型?

当n增加时,R中双循环的速度非常慢.有没有办法提高for循环的速度?

    set.seed(1)
    n=1000

    y=rnorm(n)
    x1=rnorm(n)
    x2=rnorm(n)

    lm.ft=function(y,x1,x2)
      lm.fit(cbind(1,x1.bar,x2.bar), y)$coef

    res=array(,dim=c(1,3,n,n))
    for(i in 1:n)
      for(j in 1:n){
       x1.bar=x1-x1[i]
       x2.bar=x2-x2[j]
       res[,,i,j]=lm.ft(y,x1.bar,x2.bar)
      }
Run Code Online (Sandbox Code Playgroud)

r

1
推荐指数
1
解决办法
244
查看次数

标签 统计

r ×2

c++ ×1

list ×1

rcpp ×1