[R] Create Matrix from Loop of Vectors, Sort It and Pick Top-K

From: Gundala Viswanath <gundalav_at_gmail.com>
Date: Thu, 19 Jun 2008 22:59:08 +0800

Hi,

__DATA__

300.35 200.25 104.30
22.00 31.12 89.99
444.50 22.10 43.00
22.10 200.55 66.77

Now from that I wish to do the following:

1. Compute variance of each row
2. Pick top-2 row with highest variance
3. Store those selected rows for further processing

To achieve this, I tried to: a) read the table and compute variance for each row, b) append variance with its original row in a vector, c) store a vector into multidimentional array (matrix), d) sort that array. But I am stuck at the step (b).

Can anybody suggest what's the best way to achieve my aim above?

This is the sample code I have so far (not working).

__BEGIN__

# Is this a right way to initialize?
all.arr = NULL

for (gi in 1:nofrow) {

gex <- as.vector(data.matrix(data[gi,],rownames.force=FALSE))

#compute variance
gexvar <- var(gex)

# join variance with its original vector    nvec <- c(gexvar,gex)

# I'm stuck here.....This doesn't seem to work    all.arr <- data.frame(nvec)
}

print(all.arr)
__END__

```--
Gundala Viswanath
Jakarta - Indonesia

______________________________________________

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help