[Rcpp-devel] translate an R vectorized loop with three logical conditions to C++

Nelson Villoria nvillori at purdue.edu
Fri May 11 15:06:20 CEST 2012


I am new to this list, so I hope this is the right place to ask this 
question. I am trying to translate the R vectorized loop below to C++ in 
order to speed up my calculations:

Let:
 > n1
  [1] 1 1 2 2 2 3 3 4 4 4 5 5 5 5 6 6 6 7 7 8 8 8 9 9
 > n2
  [1] 2 4 1 3 5 2 6 1 5 7 2 4 6 8 3 5 9 4 8 5 7 9 6 8
 > w1w1
  [1] 0.2500000 0.2500000 0.1111111 0.1111111 0.1111111 0.2500000 0.2500000
  [8] 0.1111111 0.1111111 0.1111111 0.0625000 0.0625000 0.0625000 0.0625000
[15] 0.1111111 0.1111111 0.1111111 0.2500000 0.2500000 0.1111111 0.1111111
[22] 0.1111111 0.2500000 0.2500000

My vectorized loop is:

     tWSWS.k <- lapply(c(1:length(n1)), function(.n1){
       lapply(c(1:length(n2)), function(.n2){
         if(.n1!=.n2){
          w1w1[n1==.n1 & n2==.n2]
        }})})

result=sum(unlist(tWSWS.k))

Could you help me with this translation or at least point me out to some 
reference/example?

Thanks!

Nelson

-- 
Nelson Villoria
Research Assistant Professor
Center for Global Trade Analysis
Department of Agricultural Economics
Purdue University
403 W. State St
West Lafayette IN 47906
765.494.4303
nvillori at purdue.edu



More information about the Rcpp-devel mailing list