jeudi 22 février 2018

Find min for each group in r

I am trying to get the FL at the minimum start time to paste in StartTimeFL for each group. I found the min start time by using the following code:

DF$StartTime<-with(DF, ave(DateTime, AlertID, FUN=min))

An example of the dataframe I have is:

AlertID     DateTime                FL     StartTime               StartTimeFL
  FI         2017-06-07 23:00:45     300    2017-06-07 23:00:45    
  FI         2017-06-07 23:00:49     400    2017-06-07 23:00:45 
  FI         2017-06-07 23:00:53     300    2017-06-07 23:00:45 
  DJ         2017-05-07 03:00:00     500    2017-05-07 03:00:00
  DJ         2017-05-07 03:00:04     400    2017-05-07 03:00:00

The final dataframe I want will look like this:

AlertID     DateTime                FL     StartTime               StartTimeFL
  FI         2017-06-07 23:00:45     300    2017-06-07 23:00:45     300    
  FI         2017-06-07 23:00:49     400    2017-06-07 23:00:45     300
  FI         2017-06-07 23:00:53     300    2017-06-07 23:00:45     300
  DJ         2017-05-07 03:00:00     500    2017-05-07 03:00:00     500
  DJ         2017-05-07 03:00:04     400    2017-05-07 03:00:00     500

The code I am using so far (as shown below) only puts the StartTimeFL in the row where the start time is for each group.

DF$StartTimeFL<-with(DF, QNHCorrectedAlt[ifelse(DateTime==StartTime, TRUE,NA)])

Any help would be great. Thanks

Aucun commentaire:

Enregistrer un commentaire