[R] Presumably simple question about sorting/ordering

Colin Beale c.beale at macaulay.ac.uk
Fri Jul 3 11:12:50 CEST 2009


Hi, 

I'm stuck with a fairly basic re-ordering problem. I want to extract part of a matrix as a vector, and reorder it to match with an erratic sequence of x,y coordinates. Here's an example that shows what I want and how close I've got (but for some reason my mind just can't get the final step at the moment!):

#Define a grid:
xy100 <- expand.grid(1:10, 1:10)
# Define a matrix ofvalues
mat <- matrix(log(xy100[,1]) * log(xy100[,2])+1, ncol = 10)/2

#Plot some the matrix with image and the same data as xy plot:
par(mfrow = c(2,2))
image(mat)
plot(xy100, cex = c(mat), pch = 20)

#define the subset of coordinates I'm interested in:
shortXY <- expand.grid(1:5, 1:5)
#and shuffle these into an erratic sequence:
rand.seq <- sample(1:25)
shortXY <- shortXY[rand.seq,]

#Identify which coordinates in the full dataset are needed (not doubt better ways exist!):
needed <- which(paste(xy100[,1],xy100[,2], sep = " ") %in% paste(shortXY[,1], shortXY[,2], sep = " "))
#Sort these into a sensible order:
sorted <- needed[order(xy100[needed,1], xy100[needed,2])]

# plot showing that when the short-coordinates are ordered (removing the erratic sequence)
# the sorted subset of the matrix is correctly subsetted:
plot(shortXY[order(shortXY[,1], shortXY[,2]),], cex = c(mat)[sorted], pch = 20)

#but what further ordering do I apply to the
# sorted and subsetted matrix, to mean that I don't have to alter the 
# sequence of the shortXY coordinate list? In this example it is obviously the value of rand.seq:
plot(shortXY, cex = c(mat)[sorted][rand.seq], pch = 20)
# but what is the general value of this: how can I recover rand.seq when it isn't known?

Obviously I could do some of this (much more elegantly) with merge, but for various reasons I don't want to (I need to run the process many, many times on very large datasets). Thanks for any pointers - I'm being very slow today...

Colin

PS sessionInfo()
R version 2.8.1 (2008-12-22) 
i386-pc-mingw32 

locale:
LC_COLLATE=English_United Kingdom.1252;LC_CTYPE=English_United Kingdom.1252;LC_MONETARY=English_United Kingdom.1252;LC_NUMERIC=C;LC_TIME=English_United Kingdom.1252

attached base packages:
[1] stats     graphics  grDevices datasets  tcltk     utils     methods   base     

other attached packages:
[1] debug_1.1.0    mvbutils_1.1.1 svSocket_0.9-5 svIO_0.9-5     R2HTML_1.58    svMisc_0.9-5   svIDE_0.9-5   

loaded via a namespace (and not attached):
[1] tools_2.8.1



-- 
Please note that the views expressed in this e-mail are those of the
sender and do not necessarily represent the views of the Macaulay
Institute. This email and any attachments are confidential and are
intended solely for the use of the recipient(s) to whom they are
addressed. If you are not the intended recipient, you should not read,
copy, disclose or rely on any information contained in this e-mail, and
we would ask you to contact the sender immediately and delete the email
from your system. Thank you.
Macaulay Institute and Associated Companies, Macaulay Drive,
Craigiebuckler, Aberdeen, AB15 8QH.




More information about the R-help mailing list