[R] Order a vector and move to new vector

Nandi nands31 at gmail.com
Thu Sep 10 20:23:27 CEST 2009


Try the following:

Data <- read.table('Sample.txt', header=T)
> Data
     ID    UNAV
1  1694  999.89
2  1696  999.90
3  1694  999.87
4  1696  999.88
5  1694  999.84
6  1696  999.86
7  1694  999.82
8  1696  999.84
9  1694  999.79
10 1696  999.82
11 1694  999.71
12 1696  999.75
13 1694  999.69
14 1696  999.73
15 1694  999.66
16 1696  999.71
17 1694  999.64
18 1696  999.69
19 1694  999.61
20 1696  999.67
21 1694  999.54
22 1696  999.70
23 1694  999.51
24 1696 1002.24
25 1694  999.48
26 1696 1012.14
27 1694  999.46
28 1696 1003.38
29 1694  999.43

Then type:
Data_Ordered <- Data[order(Data$ID),]
> Data_Ordered
     ID    UNAV
1  1694  999.89
3  1694  999.87
5  1694  999.84
7  1694  999.82
9  1694  999.79
11 1694  999.71
13 1694  999.69
15 1694  999.66
17 1694  999.64
19 1694  999.61
21 1694  999.54
23 1694  999.51
25 1694  999.48
27 1694  999.46
29 1694  999.43
2  1696  999.90
4  1696  999.88
6  1696  999.86
8  1696  999.84
10 1696  999.82
12 1696  999.75
14 1696  999.73
16 1696  999.71
18 1696  999.69
20 1696  999.67
22 1696  999.70
24 1696 1002.24
26 1696 1012.14
28 1696 1003.38

Not sure if this is what you were looking for.

-Nandi



On Sep 10, 4:06 am, Conrad Addo <conrad.a... at gmail.com> wrote:
> I currently have a data frame with a Fund ID and NAV value.  Is it possible
> to order the data frame and move to separate columns in a new data frame or
> matrix in R without using a for loop?  I suppose I'd like to use a built in
> function to make it faster because I will have around 60,000 entries to sort
> and my current program takes too long to do this.  I know how to use order,
> but is there a way to separate the result and place into new vectors of a
> matrix or data frame?
>
> Thanks
>
> Conrad
>
> Here are my vectors which I import from a csv file:
>
>  ID UNAV  #1694 999.89  #1696 999.9  #1694 999.87  #1696 999.88  #1694
> 999.84  #1696 999.86  #1694 999.82  #1696 999.84  #1694 999.79  #1696 999.82
> #1694 999.71  #1696 999.75  #1694 999.69  #1696 999.73  #1694 999.66  #1696
> 999.71  #1694 999.64  #1696 999.69  #1694 999.61  #1696 999.67  #1694 999.54
> #1696 999.7  #1694 999.51  #1696 1002.24  #1694 999.48  #1696 1012.14  #1694
> 999.46  #1696 1003.38  #1694 999.43
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-h... at r-project.org mailing listhttps://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list