如何创建每n行val的平均值并保留相应的帧和(最低)年份索引?预期输出
frame year val
Csb 1990 Mean(row1,row2)
Csb 1992 Mean(row3,row4)
Csb 1994 Mean(row5,row6)
Csb 1996 Mean(row7,row8)
..
BSk 1990 Mean(row1,row2)
BSk 1992 Mean(row3,row4)
BSk 1994 Mean(row5,row6)
BSk 1996 Mean(row7,row8)
这是数据:
structure(list(frame = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L,
5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L), .Label = c("Csb", "BSk",
"Dfb(W)", "Dfb(E)", "Dfa", "Cfa"), class = "factor"), 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, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
2011, 2012, 2013, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
2009, 2010, 2011, 2012, 2013, 1990, 1991, 1992, 1993, 1994, 1995,
1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
2007, 2008, 2009, 2010, 2011, 2012, 2013, 1990, 1991, 1992, 1993,
1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 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(0.212121212121212, 0.1, 0.15, 0.233333333333333, 0.0967741935483871,
0.185430463576159, 0.218934911242604, 0.252873563218391, 0.185840707964602,
0.135135135135135, 0.333333333333333, 0.0571428571428571, 0.166666666666667,
0.134615384615385, 0.147058823529412, 0.226890756302521, 0.113821138211382,
0.177777777777778, 0.192307692307692, 0.105263157894737, 0.0588235294117647,
0.10377358490566, 0.201612903225806, 0.2, 0.571428571428571,
0.193548387096774, 0.25, 0.226666666666667, 0.416666666666667,
0.2, 0.319148936170213, 0.203389830508475, 0.216216216216216,
0.362068965517241, 0.384615384615385, 0.611111111111111, 0.21875,
0.391304347826087, 0.377049180327869, 0.264705882352941, 0.32,
0.26530612244898, 0.296875, 0.111111111111111, 0.181818181818182,
0.225806451612903, 0.24, 0.387096774193548, 0.409090909090909,
0.705882352941177, 0.75, 0.428571428571429, 0.666666666666667,
0.413333333333333, 0.423076923076923, 0.262135922330097, 0.419354838709677,
0.542857142857143, 0.785714285714286, 0.571428571428571, 0.421052631578947,
0.46875, 1, 0.357142857142857, 0.459459459459459, 0.882352941176471,
0.350877192982456, 0.521739130434783, 0.363636363636364, 0.298136645962733,
0.428571428571429, 0.333333333333333, 0.0553846153846154, 0.030188679245283,
0.0308880308880309, 0.0434782608695652, 0.0566666666666667, 0.03125,
0.0668896321070234, 0.0666666666666667, 0.0954907161803713, 0.0520446096654275,
0.0532994923857868, 0.0660660660660661, 0.0505050505050505, 0.0523917995444191,
0.0705882352941176, 0.0674157303370786, 0.0710571923743501, 0.0674157303370786,
0.0609375, 0.0591259640102828, 0.0773809523809524, 0.108935128518972,
0.0675675675675676, 0.0977777777777778, 0.125391849529781, 0.10377358490566,
0.105809128630705, 0.156846473029046, 0.109792284866469, 0.11037527593819,
0.0893333333333333, 0.0851063829787234, 0.132502831257078, 0.117056856187291,
0.139393939393939, 0.122972972972973, 0.177189409368635, 0.106930693069307,
0.131805157593123, 0.0912778904665314, 0.0927272727272727, 0.0912820512820513,
0.16519374575119, 0.189542483660131, 0.164199192462988, 0.0950819672131148,
0.0970873786407767, 0.178321678321678, 0.0884210526315789, 0.0669330669330669,
0.0670391061452514, 0.0980810234541578, 0.0879556259904913, 0.0786782061369001,
0.117303609341826, 0.0756302521008403, 0.0973747016706444, 0.1215953307393,
0.0945790080738178, 0.10979020979021, 0.104727707959306, 0.0896797153024911,
0.0981941309255079, 0.099515868746638, 0.100828729281768, 0.129787234042553,
0.127405096203848, 0.0811320754716981, 0.0686274509803922, 0.103448275862069,
0.0819521178637201, 0.0750782064650678)), .Names = c("frame",
"year", "val"), row.names = c(3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L,
24L, 25L, 26L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L,
39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L,
52L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L,
68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 83L,
84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L,
97L, 98L, 99L, 100L, 101L, 102L, 103L, 104L, 105L, 106L, 112L,
113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L,
124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 134L,
135L, 148L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L,
158L, 159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L, 168L,
169L, 170L, 171L), class = "data.frame")
答案 0 :(得分:1)
我想这就是你要找的东西
require(data.table)
setDT(df)[,.(val=mean(val)), by = .(frame,year-0:1)]
这给了你:
frame year val
1: Csb 1990 0.15606061
2: Csb 1992 0.19166667
3: Csb 1994 0.14110233
4: Csb 1996 0.23590424
...
这个答案建立于: How can I get mean of every n rows and keep the date index?
<强> BUT 强>
请亲自查看data.table
包,了解它的工作原理,而不仅仅是从现在开始询问每一步。
答案 1 :(得分:0)
使用plyr包的另一个选项是
library(plyr)
ddply(data, .(frame, year-0:1), summarize, mean=mean(val))