How to get column mean for specific rows only?
Since [proved wrong, thanks @DWin]. For completeness the findInterval
requires year
to be sorted (as it is in your example) I'd be tempted to use cut
in case it isn't sorteddata.table
equivalent (scales for large data) is :
require(data.table)
DT = as.data.table(DF) # or just start with a data.table in the first place
DT[, mean:=mean(score), by=cut(year,c(-Inf,1984,1991,Inf))]
or findInterval
is likely faster as DWin used :
DT[, mean:=mean(score), by=findInterval(year,c(-Inf,1984,1991,Inf))]
datfrm$mean <-
with (datfrm, ave( score, findInterval(year, c(-Inf, 1984, 1991, Inf)), FUN= mean) )
The title question is a bit different than the real question and would be answered by using logical indexing. If one wanted only the mean for a particular subset say year >= 1984 & year <= 1990
it would be done via:
mn84_90 <- with(datfrm, mean(score[year >= 1984 & year <= 1990]) )