How to get column mean for specific rows only?

前端 未结 3 2088
不知归路
不知归路 2020-12-30 09:05

I need to get the mean of one column (here: score) for specific rows (here: years). Specifically, I would like to know the average score for three periods:

  • per
3条回答
  •  情歌与酒
    2020-12-30 09:28

    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]) )
    

提交回复
热议问题