我有以下数据表:
dt.data <- structure(list(delivMonth = c("2024-04", "2024-04", "2024-04",
"2024-04", "2024-04", "2024-04", "2024-04", "2024-04", "2024-04",
"2024-04", "2024-04", "2024-04", "2024-04", "2024-04", "2024-04",
"2024-04", "2024-04", "2024-04", "2024-04", "2024-04", "2024-04",
"2024-04", "2024-04", "2024-04", "2024-04", "2024-04", "2024-04",
"2024-04", "2024-04", "2024-04", "2024-04", "2024-04", "2024-04",
"2024-04", "2024-04", "2024-04", "2024-04", "2024-04", "2024-05",
"2024-05", "2024-05", "2024-05", "2024-05", "2024-05", "2024-05",
"2024-05", "2024-05", "2024-05", "2024-05", "2024-05", "2024-05",
"2024-05", "2024-05", "2024-05", "2024-05", "2024-05", "2024-05",
"2024-05", "2024-05", "2024-05", "2024-05", "2024-05", "2024-05",
"2024-05", "2024-05", "2024-05", "2024-05", "2024-05", "2024-05",
"2024-05", "2024-05", "2024-05", "2024-06", "2024-06", "2024-06",
"2024-06", "2024-06", "2024-06", "2024-06", "2024-06", "2024-06",
"2024-06", "2024-06", "2024-06", "2024-06", "2024-06", "2024-06",
"2024-06", "2024-06", "2024-06", "2024-06", "2024-06", "2024-06",
"2024-06", "2024-06", "2024-06", "2024-06", "2024-06", "2024-06",
"2024-06", "2024-06", "2024-06", "2024-06", "2024-06", "2024-06",
"2024-06", "2024-07", "2024-07", "2024-07", "2024-07", "2024-07",
"2024-07", "2024-07", "2024-07", "2024-07", "2024-07", "2024-07",
"2024-07", "2024-07", "2024-07", "2024-07", "2024-07", "2024-07",
"2024-07", "2024-07", "2024-07", "2024-07", "2024-07", "2024-07",
"2024-07", "2024-07", "2024-07", "2024-07", "2024-07", "2024-07",
"2024-07", "2024-07", "2024-07", "2024-07", "2024-07", "2024-07",
"2024-07", "2024-07", "2024-08", "2024-08", "2024-08", "2024-08",
"2024-08", "2024-08", "2024-08", "2024-08", "2024-08", "2024-08",
"2024-08", "2024-08", "2024-08", "2024-08", "2024-08", "2024-08",
"2024-08", "2024-08", "2024-08", "2024-08", "2024-08", "2024-08",
"2024-08", "2024-08", "2024-08", "2024-08", "2024-08", "2024-08",
"2024-08", "2024-08", "2024-08", "2024-08", "2024-08", "2024-08",
"2024-08", "2024-08", "2024-08", "2024-09", "2024-09", "2024-09",
"2024-09", "2024-09", "2024-09", "2024-09", "2024-09", "2024-09",
"2024-09", "2024-09", "2024-09", "2024-09", "2024-09", "2024-09",
"2024-09", "2024-09", "2024-09", "2024-09", "2024-09", "2024-09",
"2024-09", "2024-09", "2024-09", "2024-09", "2024-09", "2024-09",
"2024-09", "2024-09", "2024-09", "2024-09", "2024-09", "2024-09",
"2024-09", "2024-09", "2024-09", "2024-09", "2024-10", "2024-10",
"2024-10", "2024-10", "2024-10", "2024-10", "2024-10", "2024-10",
"2024-10", "2024-10", "2024-10", "2024-10", "2024-10", "2024-10",
"2024-10", "2024-10", "2024-10", "2024-10", "2024-10", "2024-10",
"2024-10", "2024-10", "2024-10", "2024-10", "2024-10", "2024-10",
"2024-10", "2024-10", "2024-10", "2024-10", "2024-10", "2024-10",
"2024-11", "2024-11", "2024-11", "2024-11", "2024-11", "2024-11",
"2024-11", "2024-11", "2024-11", "2024-11", "2024-11", "2024-11",
"2024-11", "2024-11", "2024-11", "2024-11", "2024-11", "2024-11",
"2024-11", "2024-11", "2024-11", "2024-11", "2024-11", "2024-11",
"2024-11", "2024-11", "2024-11", "2024-11", "2024-11", "2024-11",
"2024-11", "2024-11", "2024-12", "2024-12", "2024-12", "2024-12",
"2024-12", "2024-12", "2024-12", "2024-12", "2024-12", "2024-12",
"2024-12", "2024-12", "2024-12", "2024-12", "2024-12", "2024-12",
"2024-12", "2024-12", "2024-12"), quantity = c(-3600, 1440, 0,
-34560, 5760, 0, 7200, 3600, 720, 993.6, 1800, 734.4, 720, -13680,
18252, -842.4, 2880, -3600, -2160, 3600, -3600, 43272, 0, 3600,
-1420.56, 20534.4, 835.2, 345.6, 2160, -7200, -14400, 5040, 720,
-10800, 360, 3600, 1980, 720, -3720, 1488, 0, -35712, 5952, 0,
-7440, 744, 1026.72, 1860, 758.88, 744, -4464, 18860.4, -870.48,
2976, -2232, 3720, -3720, 33554.4, 0, -1467.91, 15266.88, 863.04,
357.12, 2232, -7440, -3720, 5208, 744, 372, 3720, 2046, 744,
-3600, 1440, 0, -34560, 5760, 0, -7200, 720, 993.6, 1800, 734.4,
720, -720, 18252, -842.4, 2880, -2160, 3600, -3600, 32472, 0,
-1420.56, 11174.4, 835.2, 345.6, 2160, -7200, -3600, 5040, 720,
360, 3600, 1980, 720, -3720, 1488, -3720, -35712, 5952, 0, -3720,
3720, 744, 1026.72, 1860, 758.88, 744, -744, 18860.4, -870.48,
2976, 0, -2232, 3720, -3720, 32810.4, 0, -1467.91, 11546.88,
863.04, 357.12, -1488, -7440, -7440, 5208, 744, 372, 3720, 3720,
2046, 744, -3720, 1488, -3720, -35712, 5952, 0, -3720, 3720,
744, 1026.72, 1860, 758.88, 744, -744, 18860.4, -870.48, 2976,
0, -2232, 3720, -3720, 32810.4, 0, -1467.91, 11546.88, 863.04,
357.12, -1488, -7440, -7440, 5208, 744, 372, 3720, 3720, 2046,
744, -3600, 1440, -3600, -34560, 5760, 0, -3600, 3600, 720, 993.6,
1800, 734.4, 720, -4320, 18252, -842.4, 2880, 0, -2160, 3600,
-3600, 31752, 0, -1420.56, 14774.4, 835.2, 345.6, -1440, -7200,
-7200, 5040, 720, 360, 3600, 3600, 1980, 720, 35015, 1490, 0,
21605, 5960, 0, 4470, 1028.1, 1862.5, 759.9, 745, -2980, 18885.75,
-871.65, 2980, 3725, -3725, 32854.5, 0, -1469.89, 12307.4, 864.2,
357.6, 12665, -7450, -3725, 5215, 745, 372.5, 3725, 2048.75,
745, 33840, 1440, 0, 20880, 5760, 0, 4320, 993.6, 1800, 734.4,
720, -7200, 18252, -842.4, 2880, 3600, -3600, 31752, 0, -1420.56,
11894.4, 835.2, 345.6, 12240, -7200, -3600, 5040, 720, 360, 3600,
1980, 720, 34968, 1488, 0, 21576, 5952, 0, 4464, 1026.72, 1860,
758.88, 744, -22320, 18860.4, -870.48, 2976, 3720, -3720, 32810.4,
0), counterparty = c("Axpo (CH)", "Axpo (CH)", "Axpo (CH)",
"CEZ (CZ)", "CEZ (CZ)", "CEZ (CZ)", "DXT Commodities (CH)", "DXT Commodities (CH)",
"EDF Trading (GB)", "EDF Trading (GB)", "EDF Trading (GB)", "EnBW (DE)",
"Energie AG (AT)", "Energie Klagenfurt (AT)", "Engie (FR)", "Engie (FR)",
"Gunvor (CH)", "Gunvor (CH)", "HSE (SI)", "Mercuria (CH)", "Mercuria (CH)",
"OMV Gas M&T (AT)", "OMV Gas M&T (AT)", "OMV Gas M&T (AT)", "RAG (AT)",
"RWE (DE)", "RWE (DE)", "RWE (DE)", "RWE (GB)", "RWE (GB)", "RWE (GB)",
"SEFE (GB)", "SEFE (GB)", "Shell (GB)", "Uniper (DE)", "Uniper (DE)",
"WINGAS (DE)", "WINGAS (DE)", "Axpo (CH)", "Axpo (CH)", "Axpo (CH)",
"CEZ (CZ)", "CEZ (CZ)", "CEZ (CZ)", "DXT Commodities (CH)", "EDF Trading (GB)",
"EDF Trading (GB)", "EDF Trading (GB)", "EnBW (DE)", "Energie AG (AT)",
"Energie Klagenfurt (AT)", "Engie (FR)", "Engie (FR)", "Gunvor (CH)",
"HSE (SI)", "Mercuria (CH)", "Mercuria (CH)", "OMV Gas M&T (AT)",
"OMV Gas M&T (AT)", "RAG (AT)", "RWE (DE)", "RWE (DE)", "RWE (DE)",
"RWE (GB)", "RWE (GB)", "RWE (GB)", "SEFE (GB)", "SEFE (GB)",
"Uniper (DE)", "Uniper (DE)", "WINGAS (DE)", "WINGAS (DE)", "Axpo (CH)",
"Axpo (CH)", "Axpo (CH)", "CEZ (CZ)", "CEZ (CZ)", "CEZ (CZ)",
"DXT Commodities (CH)", "EDF Trading (GB)", "EDF Trading (GB)",
"EDF Trading (GB)", "EnBW (DE)", "Energie AG (AT)", "Energie Klagenfurt (AT)",
"Engie (FR)", "Engie (FR)", "Gunvor (CH)", "HSE (SI)", "Mercuria (CH)",
"Mercuria (CH)", "OMV Gas M&T (AT)", "OMV Gas M&T (AT)", "RAG (AT)",
"RWE (DE)", "RWE (DE)", "RWE (DE)", "RWE (GB)", "RWE (GB)", "RWE (GB)",
"SEFE (GB)", "SEFE (GB)", "Uniper (DE)", "Uniper (DE)", "WINGAS (DE)",
"WINGAS (DE)", "Axpo (CH)", "Axpo (CH)", "Axpo (CH)", "CEZ (CZ)",
"CEZ (CZ)", "CEZ (CZ)", "DXT Commodities (CH)", "DXT Commodities (CH)",
"EDF Trading (GB)", "EDF Trading (GB)", "EDF Trading (GB)", "EnBW (DE)",
"Energie AG (AT)", "Energie Klagenfurt (AT)", "Engie (FR)", "Engie (FR)",
"Gunvor (CH)", "Gunvor (CH)", "HSE (SI)", "Mercuria (CH)", "Mercuria (CH)",
"OMV Gas M&T (AT)", "OMV Gas M&T (AT)", "RAG (AT)", "RWE (DE)",
"RWE (DE)", "RWE (DE)", "RWE (GB)", "RWE (GB)", "RWE (GB)", "SEFE (GB)",
"SEFE (GB)", "Uniper (DE)", "Uniper (DE)", "Vitol (CH)", "WINGAS (DE)",
"WINGAS (DE)", "Axpo (CH)", "Axpo (CH)", "Axpo (CH)", "CEZ (CZ)",
"CEZ (CZ)", "CEZ (CZ)", "DXT Commodities (CH)", "DXT Commodities (CH)",
"EDF Trading (GB)", "EDF Trading (GB)", "EDF Trading (GB)", "EnBW (DE)",
"Energie AG (AT)", "Energie Klagenfurt (AT)", "Engie (FR)", "Engie (FR)",
"Gunvor (CH)", "Gunvor (CH)", "HSE (SI)", "Mercuria (CH)", "Mercuria (CH)",
"OMV Gas M&T (AT)", "OMV Gas M&T (AT)", "RAG (AT)", "RWE (DE)",
"RWE (DE)", "RWE (DE)", "RWE (GB)", "RWE (GB)", "RWE (GB)", "SEFE (GB)",
"SEFE (GB)", "Uniper (DE)", "Uniper (DE)", "Vitol (CH)", "WINGAS (DE)",
"WINGAS (DE)", "Axpo (CH)", "Axpo (CH)", "Axpo (CH)", "CEZ (CZ)",
"CEZ (CZ)", "CEZ (CZ)", "DXT Commodities (CH)", "DXT Commodities (CH)",
"EDF Trading (GB)", "EDF Trading (GB)", "EDF Trading (GB)", "EnBW (DE)",
"Energie AG (AT)", "Energie Klagenfurt (AT)", "Engie (FR)", "Engie (FR)",
"Gunvor (CH)", "Gunvor (CH)", "HSE (SI)", "Mercuria (CH)", "Mercuria (CH)",
"OMV Gas M&T (AT)", "OMV Gas M&T (AT)", "RAG (AT)", "RWE (DE)",
"RWE (DE)", "RWE (DE)", "RWE (GB)", "RWE (GB)", "RWE (GB)", "SEFE (GB)",
"SEFE (GB)", "Uniper (DE)", "Uniper (DE)", "Vitol (CH)", "WINGAS (DE)",
"WINGAS (DE)", "Axpo (CH)", "Axpo (CH)", "Axpo (CH)", "CEZ (CZ)",
"CEZ (CZ)", "CEZ (CZ)", "EDF Trading (GB)", "EDF Trading (GB)",
"EDF Trading (GB)", "EnBW (DE)", "Energie AG (AT)", "Energie Klagenfurt (AT)",
"Engie (FR)", "Engie (FR)", "Gunvor (CH)", "Mercuria (CH)", "Mercuria (CH)",
"OMV Gas M&T (AT)", "OMV Gas M&T (AT)", "RAG (AT)", "RWE (DE)",
"RWE (DE)", "RWE (DE)", "RWE (GB)", "RWE (GB)", "RWE (GB)", "SEFE (GB)",
"SEFE (GB)", "Uniper (DE)", "Uniper (DE)", "WINGAS (DE)", "WINGAS (DE)",
"Axpo (CH)", "Axpo (CH)", "Axpo (CH)", "CEZ (CZ)", "CEZ (CZ)",
"CEZ (CZ)", "EDF Trading (GB)", "EDF Trading (GB)", "EDF Trading (GB)",
"EnBW (DE)", "Energie AG (AT)", "Energie Klagenfurt (AT)", "Engie (FR)",
"Engie (FR)", "Gunvor (CH)", "Mercuria (CH)", "Mercuria (CH)",
"OMV Gas M&T (AT)", "OMV Gas M&T (AT)", "RAG (AT)", "RWE (DE)",
"RWE (DE)", "RWE (DE)", "RWE (GB)", "RWE (GB)", "RWE (GB)", "SEFE (GB)",
"SEFE (GB)", "Uniper (DE)", "Uniper (DE)", "WINGAS (DE)", "WINGAS (DE)",
"Axpo (CH)", "Axpo (CH)", "Axpo (CH)", "CEZ (CZ)", "CEZ (CZ)",
"CEZ (CZ)", "EDF Trading (GB)", "EDF Trading (GB)", "EDF Trading (GB)",
"EnBW (DE)", "Energie AG (AT)", "Energie Klagenfurt (AT)", "Engie (FR)",
"Engie (FR)", "Gunvor (CH)", "Mercuria (CH)", "Mercuria (CH)",
"OMV Gas M&T (AT)", "OMV Gas M&T (AT)"), marketArea = c("CEGH",
"THE", "TTF", "CEGH", "THE", "TTF", "CEGH", "TTF", "CEGH", "THE",
"TTF", "TTF", "CEGH", "CEGH", "CEGH", "THE", "THE", "TTF", "CEGH",
"CEGH", "TTF", "CEGH", "THE", "TTF", "CEGH", "CEGH", "THE", "TTF",
"CEGH", "THE", "TTF", "CEGH", "THE", "TTF", "THE", "TTF", "CEGH",
"TTF", "CEGH", "THE", "TTF", "CEGH", "THE", "TTF", "CEGH", "CEGH",
"THE", "TTF", "TTF", "CEGH", "CEGH", "CEGH", "THE", "THE", "CEGH",
"CEGH", "TTF", "CEGH", "THE", "CEGH", "CEGH", "THE", "TTF", "CEGH",
"THE", "TTF", "CEGH", "THE", "THE", "TTF", "CEGH", "TTF", "CEGH",
"THE", "TTF", "CEGH", "THE", "TTF", "CEGH", "CEGH", "THE", "TTF",
"TTF", "CEGH", "CEGH", "CEGH", "THE", "THE", "CEGH", "CEGH",
"TTF", "CEGH", "THE", "CEGH", "CEGH", "THE", "TTF", "CEGH", "THE",
"TTF", "CEGH", "THE", "THE", "TTF", "CEGH", "TTF", "CEGH", "THE",
"TTF", "CEGH", "THE", "TTF", "CEGH", "TTF", "CEGH", "THE", "TTF",
"TTF", "CEGH", "CEGH", "CEGH", "THE", "THE", "TTF", "CEGH", "CEGH",
"TTF", "CEGH", "THE", "CEGH", "CEGH", "THE", "TTF", "CEGH", "THE",
"TTF", "CEGH", "THE", "THE", "TTF", "TTF", "CEGH", "TTF", "CEGH",
"THE", "TTF", "CEGH", "THE", "TTF", "CEGH", "TTF", "CEGH", "THE",
"TTF", "TTF", "CEGH", "CEGH", "CEGH", "THE", "THE", "TTF", "CEGH",
"CEGH", "TTF", "CEGH", "THE", "CEGH", "CEGH", "THE", "TTF", "CEGH",
"THE", "TTF", "CEGH", "THE", "THE", "TTF", "TTF", "CEGH", "TTF",
"CEGH", "THE", "TTF", "CEGH", "THE", "TTF", "CEGH", "TTF", "CEGH",
"THE", "TTF", "TTF", "CEGH", "CEGH", "CEGH", "THE", "THE", "TTF",
"CEGH", "CEGH", "TTF", "CEGH", "THE", "CEGH", "CEGH", "THE",
"TTF", "CEGH", "THE", "TTF", "CEGH", "THE", "THE", "TTF", "TTF",
"CEGH", "TTF", "CEGH", "THE", "TTF", "CEGH", "THE", "TTF", "CEGH",
"THE", "TTF", "TTF", "CEGH", "CEGH", "CEGH", "THE", "THE", "CEGH",
"TTF", "CEGH", "THE", "CEGH", "CEGH", "THE", "TTF", "CEGH", "THE",
"TTF", "CEGH", "THE", "THE", "TTF", "CEGH", "TTF", "CEGH", "THE",
"TTF", "CEGH", "THE", "TTF", "CEGH", "THE", "TTF", "TTF", "CEGH",
"CEGH", "CEGH", "THE", "THE", "CEGH", "TTF", "CEGH", "THE", "CEGH",
"CEGH", "THE", "TTF", "CEGH", "THE", "TTF", "CEGH", "THE", "THE",
"TTF", "CEGH", "TTF", "CEGH", "THE", "TTF", "CEGH", "THE", "TTF",
"CEGH", "THE", "TTF", "TTF", "CEGH", "CEGH", "CEGH", "THE", "THE",
"CEGH", "TTF", "CEGH", "THE")), row.names = c(NA, -300L), class = c("data.table",
"data.frame"))
现在我只想总结每个
quantity
和 counterparty
的两个特定 marketArea
的 delivDate
。我想总结 quantity
= RWE (DE)和
counterparty
= RWE (GB)。所有其他数据表条目应保持不变。 我已经尝试了很多版本的summary()、group_by()等,但我不明白。
我该如何处理这个问题?
counterparty
而不是
data.table
,因此您应该查看 tibble
数据整理包而不是 data.table
函数。一个很好的介绍在这里:https://cran.r-project.org/web/packages/data.table/vignettes/datatable-intro.html如果我理解正确的话,您想通过求和来合并
tidyverse
和
RWE (DE)
的条目。一种方法是将该交易对手重命名为(例如)RWE (GB)
,然后根据新的交易对手变量对数据表求和。
所以:RWE (DE/GB)
这将为您提供 276 行,而不是最初的 300 行,并且数量按照您的预期求和:
# First assign RWE (DE) and RWE (GB) to a new counterparty called RWE (DE/GB)
dt.data[ counterparty %in% c("RWE (DE)", "RWE (GB)"), counterparty := "RWE (DE/GB)"]
# Now sum quantity over counterparty, date and market area:
dt.data2 = dt.data[ ,.(quantity=sum(quantity)), by=.(counterparty,delivMonth, marketArea)]
> dt.data2
counterparty delivMonth marketArea quantity
<char> <char> <char> <num>
1: Axpo (CH) 2024-04 CEGH -3600.0
2: Axpo (CH) 2024-04 THE 1440.0
3: Axpo (CH) 2024-04 TTF 0.0
4: CEZ (CZ) 2024-04 CEGH -34560.0
5: CEZ (CZ) 2024-04 THE 5760.0
---
272: Gunvor (CH) 2024-12 THE 2976.0
273: Mercuria (CH) 2024-12 CEGH 3720.0
274: Mercuria (CH) 2024-12 TTF -3720.0
275: OMV Gas M&T (AT) 2024-12 CEGH 32810.4
276: OMV Gas M&T (AT) 2024-12 THE 0.0
的 48 行在新数据集中已减少到 24 行:
counterparty %in% c("RWE (DE)","RWE (GB)")
求和前的数据:
> dt.data2[ counterparty == "RWE (DE/GB)"]
counterparty delivMonth marketArea quantity
<char> <char> <char> <num>
1: RWE (DE/GB) 2024-04 CEGH 22694.40
2: RWE (DE/GB) 2024-04 THE -6364.80
3: RWE (DE/GB) 2024-04 TTF -14054.40
4: RWE (DE/GB) 2024-05 CEGH 17498.88
5: RWE (DE/GB) 2024-05 THE -6576.96
6: RWE (DE/GB) 2024-05 TTF -3362.88
7: RWE (DE/GB) 2024-06 CEGH 13334.40
8: RWE (DE/GB) 2024-06 THE -6364.80
9: RWE (DE/GB) 2024-06 TTF -3254.40
10: RWE (DE/GB) 2024-07 CEGH 10058.88
11: RWE (DE/GB) 2024-07 THE -6576.96
12: RWE (DE/GB) 2024-07 TTF -7082.88
13: RWE (DE/GB) 2024-08 CEGH 10058.88
14: RWE (DE/GB) 2024-08 THE -6576.96
15: RWE (DE/GB) 2024-08 TTF -7082.88
16: RWE (DE/GB) 2024-09 CEGH 13334.40
17: RWE (DE/GB) 2024-09 THE -6364.80
18: RWE (DE/GB) 2024-09 TTF -6854.40
19: RWE (DE/GB) 2024-10 CEGH 24972.40
20: RWE (DE/GB) 2024-10 THE -6585.80
21: RWE (DE/GB) 2024-10 TTF -3367.40
22: RWE (DE/GB) 2024-11 CEGH 24134.40
23: RWE (DE/GB) 2024-11 THE -6364.80
24: RWE (DE/GB) 2024-11 TTF -3254.40
counterparty delivMonth marketArea quantity