[R] sort data.frame by specific date column

Mark Knecht markknecht at gmail.com
Sat Jul 11 05:01:07 CEST 2009


On Fri, Jul 10, 2009 at 7:44 PM, milton ruser<milton.ruser at gmail.com> wrote:
> Hi Mark,
>
> Like this?
>
> mydf<-read.table(stdin(), head=T)
> Trade PosType  EnDate EnTime  ExDate ExTime PL_Pos
> 1       1 1040107    915 1040107   1300    164
> 2       1 1040108    909 1040108   1300    184
> 3       1 1040115    921 1040115   1300     64
> 4       1 1040120   1134 1040120   1300    124
> 5       1 1040121    923 1040121   1300     84
> 6       1 1040205   1043 1040205   1300   -196
>
> mydf.sort<-mydf[order(mydf$EnDate),]
> mydf.sort
>
> Cheers
>
> milton
> brazil=toronto
>
> On Fri, Jul 10, 2009 at 10:30 PM, Mark Knecht <markknecht at gmail.com> wrote:
>>
>> I have a data.frame that was built from a number of smaller
>> data.frames with rbind. Each ssmaller data.frame bound together runs
>> over the same date ranges. The format of the whole thing looks like
>> this:
>>
>>     Trade PosType  EnDate EnTime  ExDate ExTime PL_Pos
>> 1        1       1 1040107    915 1040107   1300    164
>> 2        2       1 1040108    909 1040108   1300    184
>> 3        3       1 1040115    921 1040115   1300     64
>> 4        4       1 1040120   1134 1040120   1300    124
>> 5        5       1 1040121    923 1040121   1300     84
>> 6        6       1 1040205   1043 1040205   1300   -196
>>
>> I would like to sort the new data.frame using column 3, EnDate from
>> lowest to highest. How do I do this?
>>
>> The help file example section for ?sort doesn't seem to have any
>> examples of doing this by column so I'm wondering if this is the done
>> with some other command I haven't discovered, or am I just missing the
>> obvious at 7:30PM on a Friday night?
>>
>> Thanks,
>> Mark
>>
>> ______________________________________________
>> 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.
>
>
Precisely what I was looking for. The PosType values (1 and -1) where
in different data.frames and bound together. Now they are sorted and
intermingled as I was looking for.

The order command. Thanks!

Cheers,
Mark

     Trade PosType  EnDate EnTime  ExDate ExTime PL_Pos
1        1       1 1040107    915 1040107   1300    164
2        2       1 1040108    909 1040108   1300    184
3        3       1 1040115    921 1040115   1300     64
4        4       1 1040120   1134 1040120   1300    124
5        5       1 1040121    923 1040121   1300     84
4111     1      -1 1040127   1009 1040127   1301    104
412      2      -1 1040129    941 1040129   1301   -396
6        6       1 1040205   1043 1040205   1300   -196
7        7       1 1040211   1054 1040211   1300     14
413      3      -1 1040212    948 1040212   1301    154
8        8       1 1040217    909 1040217   1300     84
9        9       1 1040218    931 1040218   1125   -576
414      4      -1 1040219   1027 1040219   1301    824
10      10       1 1040225   1137 1040225   1300    214
11      11       1 1040226    935 1040226   1300    -36
415      5      -1 1040302    955 1040302   1301    114
12      12       1 1040303   1006 1040303   1300




More information about the R-help mailing list