mercredi 3 octobre 2018

Identify rows with matching variables and delete rows that have smaller value

I am working with data that is collected via multiple monitors in one county, everyday. I want to retain the maximum concentration for all monitors in one county for each monitored day. First, I need to identify the rows that have reported values for the same county on the same day, then remove the rows with the lowest value of the reported values. Here is a sample data frame (p2015):

County.Code  Site.Num  Date.Local  Max.Value 
3            3         1/1/2015    30
3            3         1/1/2015    45
3            3         1/1/2015    16
4            5         1/1/2015    32
4            5         1/1/2015    18
5            6         1/1/2015    8
5            7         1/1/2015    11

I want to be left with this:

County.Code  Site.Num  Date.Local  Max.Value
3            3         1/1/2015    45
4            5         1/1/2015    32
5            7         1/1/2015    11

I thought maybe an If statement using which would do the trick but it's not working. I've tried the following with no luck:

if (p2015$County.Code == p2015$Date.Local){
p2015[ , .SD[which.min(Max.Value)]]   
}

I've searched for this and was only able to find information for removing rows based on two conditions, not three. Also, I'm just plain confused on how to extract the minimum values. Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire