On Dec 17, 2011, at 2:47 AM, ali_protocol wrote:


Newbie here.  Many apologies in advance for using the incorrect lingo.
I'm new to statistics and VERY new to R.

I have a "nx2" matrix , I want to sort the values based on the average of 2 columns and put k lowest (or highest) values in bin1, second k high/ low values in bin2, and so on (bins would be of the same dimensions). I should
also know what the first index (or position) of the pair has been.

Your posting is better than some first-timers from Nabble, but you really ought to learn to post with code-created examples:

dat <- read.table(text="car-1   29  30
car-2   22      24
car-3   16      16
car-4   41      41
car-5   43      45
car-6   34      36")

for exmample in bins of 2x2


Sort in order of ascending means:
> sdat <- dat[ order(apply(dat[,2:3], 1, mean)), ]

#Create a vector that allows grouping by twos
#Could have used simpler code if we were sure there were aneven number of rows.

> rep(1:(NROW(sdat)/2 + 1), each=2, length=NROW(sdat))
#[1] 1 1 2 2 3 3

#Use split()
> split(sdat, rep(1:(NROW(sdat)/2 + 1), each=2, length=NROW(sdat)) )
$`1`
     V1 V2 V3
3 car-3 16 16
2 car-2 22 24

$`2`
     V1 V2 V3
1 car-1 29 30
6 car-6 34 36

$`3`
     V1 V2 V3
4 car-4 41 41
5 car-5 43 45


bin1
car-3   16      16
car-2   22      24
bin2
car-1   29      30
car-6   34      35
bin3
car-4   41      41
car-5   43      45


--
David Winsemius, MD
West Hartford, CT

______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to