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

From: Jorge Ivan Velez <jorgeivanvelez_at_gmail.com>
Date: Thu, 19 Jun 2008 11:20:06 -0400

Dear Gundala,

Try this:

# Data set
DF=read.table(textConnection("300.35 200.25 104.30 22.00 31.12 89.99
444.50 22.10 43.00

# Variances
VAR=apply(DF,1,var)

# Order
pos=order(VAR)

# Print VAR and pos
VAR
pos

# ordered VAR
VAR[pos]

# top-2 highest VAR
VAR[pos][3:4]

HTH, Jorge

On Thu, Jun 19, 2008 at 10:59 AM, Gundala Viswanath <gundalav_at_gmail.com> wrote:

> 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__
>
>
>
> # 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
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

[[alternative HTML version deleted]]

R-help_at_r-project.org 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. Received on Thu 19 Jun 2008 - 16:30:12 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 19 Jun 2008 - 16:30:44 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.