Select rows containing daily maximum value in R

So, I want to multiply my data frame to select rows with a daily maximum value.

Site    Year   Day     Time      Cover       Size TempChange
 ST1    2011    97      0.0     Closed      small       0.97
 ST1    2011    97      0.5     Closed      small       1.02
 ST1    2011    97      1.0     Closed      small       1.10

The data frame section above. I would like to select only the rows that have the maximum value of the TempChange variable for each Day variable. I want to do this because I'm interested in specific variables (not shown) for these specific times.

CHANGED EXAMPLE AND REQUIRED OUTPUT

Site  Day   Temp     Row
a     10    0.2     1
a     10    0.3     2
a     11    0.5     3
a     11    0.4     4
b     10    0.1     5
b     10    0.8     6
b     11    0.7     7
b     11    0.6     8
c     10    0.2     9
c     10    0.3     10
c     11    0.5     11
c     11    0.8     12

REQUESTED EXIT

Site  Day   Temp     Row
a     10    0.3     2
a     11    0.5     3
b     10    0.8     6
b     11    0.7     7
c     10    0.3     10
c     11    0.8     12

Hope this makes sense.

+5
source share
1 answer

After faffing with the raw data frame code, I realized that plyr can do this in one thing:

> df
  Day          V Z
1  97 0.26575207 1
2  97 0.09443351 2
3  97 0.88097858 3
4  98 0.62241515 4
5  98 0.61985937 5
6  99 0.06956219 6
7 100 0.86638108 7
8 100 0.08382254 8

> ddply(df,~Day,function(x){x[which.max(x$V),]})
  Day          V Z
1  97 0.88097858 3
2  98 0.62241515 4
3  99 0.06956219 6
4 100 0.86638108 7

, . :

> df
   Site Day Temp Row
1     a  10  0.2   1
2     a  10  0.3   2
3     a  11  0.5   3
4     a  11  0.4   4
5     b  10  0.1   5
6     b  10  0.8   6
7     b  11  0.7   7
8     b  11  0.6   8
9     c  10  0.2   9
10    c  10  0.3  10
11    c  11  0.5  11
12    c  11  0.8  12
> ddply(df,~Day+Site,function(x){x[which.max(x$Temp),]})
  Site Day Temp Row
1    a  10  0.3   2
2    b  10  0.8   6
3    c  10  0.3  10
4    a  11  0.5   3
5    b  11  0.7   7
6    c  11  0.8  12

, , , .

> dmax = ddply(df,~Day+Site,function(x){x[which.max(x$Temp),]})
> dmax[order(dmax$Row),]
  Site Day Temp Row
1    a  10  0.3   2
4    a  11  0.5   3
2    b  10  0.8   6
5    b  11  0.7   7
3    c  10  0.3  10
6    c  11  0.8  12
+7

All Articles