I have a dataframe with a year index and a val index.
I would like to create a mean of every n rows of val and keep the corresponding year index.
Basically, the output would be (for n=2)
year val
1990 Mean(row1,row2)
1992 Mean(row3,row4)
1994 Mean(row5,row6)
1996 Mean(row7,row8)
How can I do this?
structure(list(year = c(1990, 1991, 1992, 1993, 1994, 1995, 1996,
1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
2008, 2009, 2010, 2011, 2012, 2013), val = c(84L, 67L, 72L, 138L,
111L, 100L, 221L, 108L, 204L, 125L, 82L, 157L, 175L, 252L, 261L,
185L, 146L, 183L, 245L, 172L, 98L, 216L, 89L, 144L)), .Names = c("year",
"val"), row.names = 13:36, class = "data.frame")
See Question&Answers more detail:
os