It seems that as the comparison returns vectors of unequal length (depends on logical TRUE in x) I am breaking the intended behavior of the operation. However,

y[x]
[1] 10 3 6 8 11 1 7 12 1 5 3 8 5 6 6 0 7 4 1 5

yields what I expect with no ordering; order(y[x]) is performed on all TRUE elements. Is this a bug? I would (naively) expect that it would perform the operation on each column separately and concatenate the result like above.

In any case, is there a reasonable way to partition asymmetric results into a matrix? I considered padding each vector with NAs to max dim(x) and then cbind into a matrix (see below). Seems like a mess as I would lose vectorization. Any more elegant ideas/hints?

2 Answers
2

I am not sure why you want to keep a matrix structure with NAs. Couldn't you use a list of lists? In that case you could transform your matrices into data frames and use mapply. That's how you execute a function on each column independently in R.

Thank you for the suggestion about using lists. I'll give it a go on some real data sets - sizes in the tens of millions - and report on execution speed.
–
user1789784Dec 21 '12 at 14:27

Okay, let us know. I am not aware that there is a vectorized version of order, and if you worry about performance, you might have to write the code and the loop in a compiled language. You might want to look at the package Rcpp, which allows to write and compile C++ code in R scripts.
–
Arnaud AmzallagDec 21 '12 at 18:22

order(y[x]) is therefore working on a numeric vector. It has no memory that y and x were matrices and no ability to read your mind that it should be applied columnwise and to a matrix that was once 3 columns and that you want to pad it with NA values.

You could use is.na<- and return a list (similar to the answer posted while I was writing this)