I have a dataframe df
with columns ID
, Year
, Value1
, Value2
, Value3
and 21788928 rows. I need to subset the data by Year
and ID
and find the max Value1
in that subset saving the rest of information of that row, I need to do that for all the combinations of Year
and ID
(Year goes from 1982 to 2013,
ID is from 1 to 28371)
I was trying to do that in a double for loop:
year<-seq(1982, 2013)
cnt=1
for (i in 1:32) {
for (j in 1:28371)
A<-df[df$Year==year[i]&df$ID==j,]
maxVal[cnt,]<-A[A$Value1==max(A$Value1),]
cnt=cnt+1
}
}
but it takes way to long. Is there a more efficient way to do that? Maybe using ddply
or with
.