Git Product home page Git Product logo

Comments (3)

eddelbuettel avatar eddelbuettel commented on June 12, 2024 1

Here is a simpler version of your code, with a repair to the R part -- you want order(), not rank():

#include <RcppArmadillo.h>

// [[Rcpp::depends(RcppArmadillo)]]

// [[Rcpp::export()]]
arma::vec cRank(arma::vec x) {
    arma::uvec rankarma  = arma::stable_sort_index(x);
    arma::vec z = arma::conv_to<arma::vec>::from(rankarma);
    return z;
}

/*** R
set.seed(123)  # reproducible
tmp <- rnorm(10)
tmp
armaind <- cRank(tmp)
rind <- order(tmp)
cbind(armaind, rind)
tmp[ armaind + 1 ]
tmp[ rind ]
*/

When I run this, the right thing happens (but note the needed +1):

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

R> set.seed(123)  # reproducible

R> tmp <- rnorm(10)

R> tmp
 [1] -0.5604756 -0.2301775  1.5587083  0.0705084  0.1292877  1.7150650  0.4609162 -1.2650612 -0.6868529 -0.4456620

R> armaind <- cRank(tmp)

R> rind <- order(tmp)

R> cbind(armaind, rind)
        rind
 [1,] 7    8
 [2,] 8    9
 [3,] 0    1
 [4,] 9   10
 [5,] 1    2
 [6,] 3    4
 [7,] 4    5
 [8,] 6    7
 [9,] 2    3
[10,] 5    6

R> tmp[ armaind + 1 ]
 [1] -1.2650612 -0.6868529 -0.5604756 -0.4456620 -0.2301775  0.0705084  0.1292877  0.4609162  1.5587083  1.7150650

R> tmp[ rind ]
 [1] -1.2650612 -0.6868529 -0.5604756 -0.4456620 -0.2301775  0.0705084  0.1292877  0.4609162  1.5587083  1.7150650
R> 

from rcpparmadillo.

eddelbuettel avatar eddelbuettel commented on June 12, 2024

Try this:

  • don't return a List if you don't need it, return a vector directory
  • important: Armadillo gives you uvec, but R only has signed vectors
  • so convert rankarma to either ivec (integer) ir just vec (numeric)
    I think you are just getting a bad cast.

from rcpparmadillo.

ChongWu-Biostat avatar ChongWu-Biostat commented on June 12, 2024

You are so helpful!!!! I learned a lot from your codes.
I really appreciate that.

Thank you so much for very prompt and helpful response.

Thanks,
Chong

from rcpparmadillo.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.