[R] Bug? Index output of C functions R_qsort_I and R_qsort_int_I is not modified

Evangelos Evangelou ee224 @end|ng |rom b@th@@c@uk
Fri Apr 16 10:35:04 CEST 2021

Thanks for your input Bill.

I also realised that the second argument must be initialised to 1:n, which is not mentioned in the documentation. So, if I define
void mysort2i (int *x, int *i, int *n)


> .C("mysort2i",4:1,integer(4),4L)
[1] 1 2 3 4

[1] 0 0 0 0

[1] 4


> .C("mysort2i",4:1,1:4,4L)
[1] 1 2 3 4

[1] 4 3 2 1

[1] 4

From: Bill Dunlap <williamwdunlap using gmail.com>
Sent: 16 April 2021 04:50
To: Evangelos Evangelou <ee224 using bath.ac.uk>
Cc: r-help using r-project.org <r-help using r-project.org>
Subject: Re: [R] Bug? Index output of C functions R_qsort_I and R_qsort_int_I is not modified

R_ext/Utils.h:void R_qsort_int_I(int *iv, int *II, int i, int j);

The last 2 arguments are int, not int*.  .C() passes pointers to vectors so you cannot call this function directly from .C().


On Thu, Apr 15, 2021 at 3:15 PM Evangelos Evangelou via R-help <r-help using r-project.org<mailto:r-help using r-project.org>> wrote:
Hi all.

Reading the documentation of these two functions
"The ..._I() versions also return the sort.index() vector in I."
I can't find anything in the documentation about sort.index(), but I'm guessing that I is the index that makes the input sorted, as in R's order(). However running the following code on
R version 4.0.5 (2021-03-31) Platform: x86_64-pc-linux-gnu (64-bit)
does not give me that. It just gives me the original input.

Rlib = file.path(Sys.getenv("R_HOME"), "lib", paste0("libR", .Platform$dynlib.ext))
n = 4L
ix = n:1
i = integer(n)
cc = .C("R_qsort_int_I", ix, i, 1L, n)

I expect 4 3 2 1, but I get 0 0 0 0. Is this a bug or have I misunderstood something?


        [[alternative HTML version deleted]]

R-help using r-project.org<mailto:R-help using r-project.org> mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html<https://eur01.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=04%7C01%7Cee224%40bath.ac.uk%7C10bfc437aa4c4a1de23208d9008ad510%7C377e3d224ea1422db0ad8fcc89406b9e%7C0%7C0%7C637541418550695414%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C1000&sdata=yY8ALqCrEoBvQftAibVu7urh9%2FNHgmAGPgcauW%2Ff9Mo%3D&reserved=0>
and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]

More information about the R-help mailing list