[R] How to get the rows corresponding to the maximum of a factor

James Rome jamesrome at gmail.com
Tue May 31 20:51:57 CEST 2011


I have a data frame as follows:
MsgType    eotpd                       fn
FI     2011-05-13 01:40:00          0
FF     2011-05-13 01:39:53          0
TC     2011-05-13 01:39:45          0
FI       2011-05-14 00:58:46          1
FF     2011-05-14 00:58:46          1
FI     2011-05-15 00:48:32          2
FF     2011-05-15 00:48:21          2
TC     2011-05-15 00:48:15          2
FI     2011-05-16 02:00:01          3
FF     2011-05-16 01:59:46          3
FI     2011-05-17 02:22:05          4
FF     2011-05-17 02:21:58          4
FI     2011-05-18 01:50:35          5
FF     2011-05-18 01:50:30          5
FI     2011-05-19 02:05:24          6
FF     2011-05-19 02:05:20          6
TC     2011-05-19 02:05:19          6
FI     2011-05-13 17:04:15          8
TC     2011-05-13 17:04:04          8
FI     2011-05-16 17:32:40          9
FF     2011-05-16 17:32:19          9
TC     2011-05-16 17:32:06          9
FI     2011-05-17 18:39:42         10
FF     2011-05-17 18:39:38         10
FI     2011-05-18 17:54:55         11
FF     2011-05-18 17:54:57         11
TC     2011-05-18 17:54:50         11
FI     2011-05-19 17:26:01         12
FF     2011-05-19 17:26:01         12
TC     2011-05-19 17:25:53         12
. . .
As you can see, I do not always have all three MsgTypes for a given fn
The MsgTypes are an ordered factor: FL < FF < TC.
What I want to get is a data frame having the maximum MsgType and its
eotpd for each fn:
MsgType    eotpd                       fn
TC     2011-05-13 01:39:45          0
FF     2011-05-14 00:58:46          1
TC     2011-05-15 00:48:15          2
FF     2011-05-16 01:59:46          3
FF     2011-05-17 02:21:58          4
FF     2011-05-18 01:50:30          5
TC     2011-05-19 02:05:19          6
TC     2011-05-13 17:04:04          8
TC     2011-05-16 17:32:06          9
FF     2011-05-17 18:39:38         10
TC     2011-05-18 17:54:50         11
TC     2011-05-19 17:25:53         12
. . .

Surely there is a clever way to do this in R?

Thanks for the help,
Jim



More information about the R-help mailing list