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

Gundala Viswanath gundalav at gmail.com
Thu Jun 19 16:59:08 CEST 2008


Hi,

I have the following dataset (simplified for example).

__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__

#data <- read.table("testdata.txt")


# 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



More information about the R-help mailing list