我试图在数据框中聚合一堆数据。但我使用的方法是生成错误:
[.default
(xj,i)中的错误:无效的下标类型'关闭'
如何更正以下代码?
代码:
agpowerDMWh<-aggregate(cbind(MW, DollarsPerMWh)~Date + HourEnding + DateDateHE + Season + MonthYear + Year + Month + Day + Weekday + IsWorkDayTSX + isPeak + isExtendedPeak + isSuperPeak , data=df, mean, na.rm=TRUE)
示例数据:
df<-structure(list(DateTime = structure(c(1325402100, 1325403000,
1325403900, 1325404800, 1325405700, 1325406600, 1325407500, 1325408400,
1325409300, 1325410200, 1325411100, 1325412000, 1325412900, 1325413800,
1325414700, 1325415600, 1325416500, 1325417400, 1325418300, 1325419200,
1325420100, 1325421000, 1325421900, 1325422800, 1325423700, 1325424600,
1325425500, 1325426400, 1325427300, 1325428200, 1325429100, 1325430000,
1325430900, 1325431800, 1325432700, 1325433600, 1325434500, 1325435400,
1325436300, 1325437200, 1325438100, 1325439000, 1325439900, 1325440800,
1325441700, 1325442600, 1325443500, 1325444400), class = c("POSIXct",
"POSIXt"), tzone = ""), MW = c(0.75636, 0.73304, 0.75504, 0.74624,
0.78628, 0.7832, 0.77176, 0.77968, 0.76516, 0.80168, 0.77, 0.7986,
0.77528, 0.7722, 0.78364, 0.77352, 0.72732, 0.75196, 0.73656,
0.72336, 0.75064, 0.737, 0.74008, 0.81752, 0.81136, 0.77924,
0.79156, 0.79024, 0.78848, 0.78056, 0.79376, 0.76824, 0.8096,
0.77, 0.78716, 0.75372, 0.737, 0.72776, 0.7304, 0.7018, 0.69652,
0.71192, 0.6952, 0.70752, 0.67804, 0.71808, 0.67584, 0.68904),
DollarsPerMWh = c(40.26, 40.26, 40.26, 40.26, 34.936, 34.936,
34.936, 34.936, 32.714, 32.714, 32.714, 32.714, 27.346, 27.346,
27.346, 27.346, 26.411, 26.411, 26.411, 26.411, 26.4, 26.4,
26.4, 26.4, 26.4, 26.4, 26.4, 26.4, 25.927, 25.927, 25.927,
25.927, 26.785, 26.785, 26.785, 26.785, 24.464, 24.464, 24.464,
24.464, 18.81, 18.81, 18.81, 18.81, 18.315, 18.315, 18.315,
18.315), minute = c(15, 30, 45, 0, 15, 30, 45, 0, 15, 30,
45, 0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30,
45, 0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30,
45, 0, 15, 30, 45, 0), hour = c(0, 0, 0, 1, 1, 1, 1, 2, 2,
2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7,
7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11,
11, 11, 12), Date = structure(c(15340, 15340, 15340, 15340,
15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340,
15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340,
15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340,
15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340,
15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340), class = "Date"),
HourEnding = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4,
4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9,
9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12),
DateDateHE = c("2012-01-01 HE1", "2012-01-01 HE1", "2012-01-01 HE1",
"2012-01-01 HE1", "2012-01-01 HE2", "2012-01-01 HE2", "2012-01-01 HE2",
"2012-01-01 HE2", "2012-01-01 HE3", "2012-01-01 HE3", "2012-01-01 HE3",
"2012-01-01 HE3", "2012-01-01 HE4", "2012-01-01 HE4", "2012-01-01 HE4",
"2012-01-01 HE4", "2012-01-01 HE5", "2012-01-01 HE5", "2012-01-01 HE5",
"2012-01-01 HE5", "2012-01-01 HE6", "2012-01-01 HE6", "2012-01-01 HE6",
"2012-01-01 HE6", "2012-01-01 HE7", "2012-01-01 HE7", "2012-01-01 HE7",
"2012-01-01 HE7", "2012-01-01 HE8", "2012-01-01 HE8", "2012-01-01 HE8",
"2012-01-01 HE8", "2012-01-01 HE9", "2012-01-01 HE9", "2012-01-01 HE9",
"2012-01-01 HE9", "2012-01-01 HE10", "2012-01-01 HE10", "2012-01-01 HE10",
"2012-01-01 HE10", "2012-01-01 HE11", "2012-01-01 HE11",
"2012-01-01 HE11", "2012-01-01 HE11", "2012-01-01 HE12",
"2012-01-01 HE12", "2012-01-01 HE12", "2012-01-01 HE12"),
Season = c("Winter", "Winter", "Winter", "Winter", "Winter",
"Winter", "Winter", "Winter", "Winter", "Winter", "Winter",
"Winter", "Winter", "Winter", "Winter", "Winter", "Winter",
"Winter", "Winter", "Winter", "Winter", "Winter", "Winter",
"Winter", "Winter", "Winter", "Winter", "Winter", "Winter",
"Winter", "Winter", "Winter", "Winter", "Winter", "Winter",
"Winter", "Winter", "Winter", "Winter", "Winter", "Winter",
"Winter", "Winter", "Winter", "Winter", "Winter", "Winter",
"Winter"), MonthYear = structure(c(2012, 2012, 2012, 2012,
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012,
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012,
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012,
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012,
2012, 2012, 2012, 2012), class = "yearmon"), Year = c(2012,
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012,
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012,
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012,
2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012,
2012, 2012, 2012, 2012, 2012, 2012, 2012), Month = c(1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1), Day = 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), Weekday = c("Sunday",
"Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday",
"Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday",
"Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday",
"Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday",
"Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday",
"Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday",
"Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday",
"Sunday", "Sunday", "Sunday", "Sunday", "Sunday"), IsWorkDayTSX = c(FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE), isPeak = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), isExtendedPeak = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1), isSuperPeak = c(0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0)), .Names = c("DateTime", "MW", "DollarsPerMWh",
"minute", "hour", "Date", "HourEnding", "DateDateHE", "Season",
"MonthYear", "Year", "Month", "Day", "Weekday", "IsWorkDayTSX",
"isPeak", "isExtendedPeak", "isSuperPeak"), row.names = c(NA,
48L), class = "data.frame")
答案 0 :(得分:1)
对我而言,它正在发挥作用:
df<-structure(
list(
DateTime = structure(c(1325402100, 1325403000, 1325403900, 1325404800, 1325405700, 1325406600, 1325407500, 1325408400, 1325409300, 1325410200, 1325411100, 1325412000, 1325412900, 1325413800, 1325414700, 1325415600, 1325416500, 1325417400, 1325418300, 1325419200, 1325420100, 1325421000, 1325421900, 1325422800, 1325423700, 1325424600, 1325425500, 1325426400, 1325427300, 1325428200, 1325429100, 1325430000, 1325430900, 1325431800, 1325432700, 1325433600, 1325434500, 1325435400, 1325436300, 1325437200, 1325438100, 1325439000, 1325439900, 1325440800, 1325441700, 1325442600, 1325443500, 1325444400), class = c("POSIXct", "POSIXt"), tzone = ""),
MW = c(0.75636, 0.73304, 0.75504, 0.74624, 0.78628, 0.7832, 0.77176, 0.77968, 0.76516, 0.80168, 0.77, 0.7986, 0.77528, 0.7722, 0.78364, 0.77352, 0.72732, 0.75196, 0.73656, 0.72336, 0.75064, 0.737, 0.74008, 0.81752, 0.81136, 0.77924, 0.79156, 0.79024, 0.78848, 0.78056, 0.79376, 0.76824, 0.8096, 0.77, 0.78716, 0.75372, 0.737, 0.72776, 0.7304, 0.7018, 0.69652, 0.71192, 0.6952, 0.70752, 0.67804, 0.71808, 0.67584, 0.68904),
DollarsPerMWh = c(40.26, 40.26, 40.26, 40.26, 34.936, 34.936, 34.936, 34.936, 32.714, 32.714, 32.714, 32.714, 27.346, 27.346, 27.346, 27.346, 26.411, 26.411, 26.411, 26.411, 26.4, 26.4, 26.4, 26.4, 26.4, 26.4, 26.4, 26.4, 25.927, 25.927, 25.927, 25.927, 26.785, 26.785, 26.785, 26.785, 24.464, 24.464, 24.464, 24.464, 18.81, 18.81, 18.81, 18.81, 18.315, 18.315, 18.315, 18.315),
minute = c(15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 0, 15, 30, 45, 0),
hour = c(0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12),
Date = structure(c(15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340, 15340), class = "Date"),
HourEnding = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12),
DateDateHE = c("2012-01-01 HE1", "2012-01-01 HE1", "2012-01-01 HE1", "2012-01-01 HE1", "2012-01-01 HE2", "2012-01-01 HE2", "2012-01-01 HE2", "2012-01-01 HE2", "2012-01-01 HE3", "2012-01-01 HE3", "2012-01-01 HE3", "2012-01-01 HE3", "2012-01-01 HE4", "2012-01-01 HE4", "2012-01-01 HE4", "2012-01-01 HE4", "2012-01-01 HE5", "2012-01-01 HE5", "2012-01-01 HE5", "2012-01-01 HE5", "2012-01-01 HE6", "2012-01-01 HE6", "2012-01-01 HE6", "2012-01-01 HE6", "2012-01-01 HE7", "2012-01-01 HE7", "2012-01-01 HE7", "2012-01-01 HE7", "2012-01-01 HE8", "2012-01-01 HE8", "2012-01-01 HE8", "2012-01-01 HE8", "2012-01-01 HE9", "2012-01-01 HE9", "2012-01-01 HE9", "2012-01-01 HE9", "2012-01-01 HE10", "2012-01-01 HE10", "2012-01-01 HE10", "2012-01-01 HE10", "2012-01-01 HE11", "2012-01-01 HE11", "2012-01-01 HE11", "2012-01-01 HE11", "2012-01-01 HE12", "2012-01-01 HE12", "2012-01-01 HE12", "2012-01-01 HE12"),
Season = c("Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter", "Winter"),
MonthYear = structure(c(2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012), class = "yearmon"), Year = c(2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012, 2012),
Month = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
Day = 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, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
Weekday = c("Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday", "Sunday"),
IsWorkDayTSX = c(FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE),
isPeak = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0),
isExtendedPeak = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
isSuperPeak = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)
),
.Names = c("DateTime", "MW", "DollarsPerMWh", "minute", "hour", "Date", "HourEnding", "DateDateHE", "Season", "MonthYear", "Year", "Month", "Day", "Weekday", "IsWorkDayTSX", "isPeak", "isExtendedPeak", "isSuperPeak"),
row.names = c(NA, 48L), class = "data.frame")
agpowerDMWh<-aggregate(
cbind(MW, DollarsPerMWh)~Date +
HourEnding +
DateDateHE +
Season +
MonthYear +
Year +
Month +
Day +
Weekday +
IsWorkDayTSX +
isPeak +
isExtendedPeak +
isSuperPeak ,
data=df, mean, na.rm=TRUE)
给出
|Date | HourEnding|DateDateHE |Season | MonthYear| Year| Month| Day|Weekday |IsWorkDayTSX | isPeak| isExtendedPeak| isSuperPeak| MW| DollarsPerMWh|
|:----------|----------:|:---------------|:------|---------:|----:|-----:|---:|:-------|:------------|------:|--------------:|-----------:|-------:|-------------:|
|2012-01-01 | 1|2012-01-01 HE1 |Winter | 2012| 2012| 1| 1|Sunday |FALSE | 0| 0| 0| 0.74767| 40.260|
|2012-01-01 | 2|2012-01-01 HE2 |Winter | 2012| 2012| 1| 1|Sunday |FALSE | 0| 0| 0| 0.78023| 34.936|
|2012-01-01 | 3|2012-01-01 HE3 |Winter | 2012| 2012| 1| 1|Sunday |FALSE | 0| 0| 0| 0.78386| 32.714|
|2012-01-01 | 4|2012-01-01 HE4 |Winter | 2012| 2012| 1| 1|Sunday |FALSE | 0| 0| 0| 0.77616| 27.346|
|2012-01-01 | 5|2012-01-01 HE5 |Winter | 2012| 2012| 1| 1|Sunday |FALSE | 0| 0| 0| 0.73480| 26.411|
|2012-01-01 | 6|2012-01-01 HE6 |Winter | 2012| 2012| 1| 1|Sunday |FALSE | 0| 0| 0| 0.76131| 26.400|
|2012-01-01 | 7|2012-01-01 HE7 |Winter | 2012| 2012| 1| 1|Sunday |FALSE | 0| 0| 0| 0.79310| 26.400|
|2012-01-01 | 10|2012-01-01 HE10 |Winter | 2012| 2012| 1| 1|Sunday |FALSE | 0| 1| 0| 0.72424| 24.464|
|2012-01-01 | 11|2012-01-01 HE11 |Winter | 2012| 2012| 1| 1|Sunday |FALSE | 0| 1| 0| 0.70279| 18.810|
|2012-01-01 | 12|2012-01-01 HE12 |Winter | 2012| 2012| 1| 1|Sunday |FALSE | 0| 1| 0| 0.69025| 18.315|
|2012-01-01 | 8|2012-01-01 HE8 |Winter | 2012| 2012| 1| 1|Sunday |FALSE | 0| 1| 0| 0.78276| 25.927|
|2012-01-01 | 9|2012-01-01 HE9 |Winter | 2012| 2012| 1| 1|Sunday |FALSE | 0| 1| 0| 0.78012| 26.785|