mathematica,计算每小时的平均值
mathematica, calculate average per hour
我有大量的天气数据。它按日期和时间列出。每 10 分钟有一个值。从 600 开始到 1400
我想计算这两个值每小时的平均值。
nr ,date1, time1, val ,nr 日期, 时间, val2
谢谢
{11238, 20120530, 1300, 11290, 20120530, 1300, 141}, {11238, \
20120530, 1310, 11290, 20120530, 1310, 223}, {11238, 20120530, 1320, \
11290, 20120530, 1320, 201}, {11238, 20120530, 1330, 11290, 20120530, \
1330, 275}, {11238, 20120530, 1340, 11290, 20120530, 1340, 371}, \
{11238, 20120530, 1350, 11290, 20120530, 1350, 275}, {11238, \
20120530, 1400, 11290, 20120530, 1400, 238}, {11238, 20120531, 600, \
11290, 20120531, 600, 342}, {11238, 20120531, 610, 11290, 20120531, \
610, 238}, {11238, 20120531, 620, 11290, 20120531, 620, 438}, {11238, \
20120531, 630, 11290, 20120531, 630, 483}, {11238, 20120531, 640, \
11290, 20120531, 640, 498}, {11238, 20120531, 650, 11290, 20120531, \
650, 535}, {11238, 20120531, 700, 11290, 20120531, 700, 527}, {11238, \
20120531, 710, 11290, 20120531, 710, 461}, {11238, 20120531, 720, \
11290, 20120531, 720, 572}, {11238, 20120531, 730, 11290, 20120531, \
730, 624},
使用给定的数据格式:{nr, date1, time1, val, nr date, time, val2}
,以下代码输出日期和小时以及 val 和 val2 的平均值。
data = {
{11238, 20120530, 1300, 11290, 20120530, 1300, 141},
{11238, 20120530, 1310, 11290, 20120530, 1310, 223},
{11238, 20120530, 1320, 11290, 20120530, 1320, 201},
{11238, 20120530, 1330, 11290, 20120530, 1330, 275},
{11238, 20120530, 1340, 11290, 20120530, 1340, 371},
{11238, 20120530, 1350, 11290, 20120530, 1350, 275},
{11238, 20120530, 1400, 11290, 20120530, 1400, 238},
{11238, 20120531, 600, 11290, 20120531, 600, 342},
{11238, 20120531, 610, 11290, 20120531, 610, 238},
{11238, 20120531, 620, 11290, 20120531, 620, 438},
{11238, 20120531, 630, 11290, 20120531, 630, 483},
{11238, 20120531, 640, 11290, 20120531, 640, 498},
{11238, 20120531, 650, 11290, 20120531, 650, 535},
{11238, 20120531, 700, 11290, 20120531, 700, 527},
{11238, 20120531, 710, 11290, 20120531, 710, 461},
{11238, 20120531, 720, 11290, 20120531, 720, 572},
{11238, 20120531, 730, 11290, 20120531, 730, 624}};
(* format the time data to hourly timestamps *)
times = (
date = ToString[#2];
hour = StringPadLeft[ToString@Floor[#3/100], 2, "0"];
{date, hour}) & @@@ data;
(* join the timestamps to the data *)
datawithtimes = MapThread[Prepend, {data, times}];
(* gather the data by the timestamps *)
gathered = GatherBy[datawithtimes, First];
(* pick out the data and average *)
{StringJoin[Insert[First@#1, " ", 2], "00"],
N@Mean[#5], N@Mean[#8]} & @@ Transpose[#] & /@ gathered
{{20120530 1300, 11290., 247.667},
{20120530 1400, 11290., 238.},
{20120531 0600, 11290., 422.333},
{20120531 0700, 11290., 546.}}
我有大量的天气数据。它按日期和时间列出。每 10 分钟有一个值。从 600 开始到 1400
我想计算这两个值每小时的平均值。
nr ,date1, time1, val ,nr 日期, 时间, val2
谢谢
{11238, 20120530, 1300, 11290, 20120530, 1300, 141}, {11238, \
20120530, 1310, 11290, 20120530, 1310, 223}, {11238, 20120530, 1320, \
11290, 20120530, 1320, 201}, {11238, 20120530, 1330, 11290, 20120530, \
1330, 275}, {11238, 20120530, 1340, 11290, 20120530, 1340, 371}, \
{11238, 20120530, 1350, 11290, 20120530, 1350, 275}, {11238, \
20120530, 1400, 11290, 20120530, 1400, 238}, {11238, 20120531, 600, \
11290, 20120531, 600, 342}, {11238, 20120531, 610, 11290, 20120531, \
610, 238}, {11238, 20120531, 620, 11290, 20120531, 620, 438}, {11238, \
20120531, 630, 11290, 20120531, 630, 483}, {11238, 20120531, 640, \
11290, 20120531, 640, 498}, {11238, 20120531, 650, 11290, 20120531, \
650, 535}, {11238, 20120531, 700, 11290, 20120531, 700, 527}, {11238, \
20120531, 710, 11290, 20120531, 710, 461}, {11238, 20120531, 720, \
11290, 20120531, 720, 572}, {11238, 20120531, 730, 11290, 20120531, \
730, 624},
使用给定的数据格式:{nr, date1, time1, val, nr date, time, val2}
,以下代码输出日期和小时以及 val 和 val2 的平均值。
data = {
{11238, 20120530, 1300, 11290, 20120530, 1300, 141},
{11238, 20120530, 1310, 11290, 20120530, 1310, 223},
{11238, 20120530, 1320, 11290, 20120530, 1320, 201},
{11238, 20120530, 1330, 11290, 20120530, 1330, 275},
{11238, 20120530, 1340, 11290, 20120530, 1340, 371},
{11238, 20120530, 1350, 11290, 20120530, 1350, 275},
{11238, 20120530, 1400, 11290, 20120530, 1400, 238},
{11238, 20120531, 600, 11290, 20120531, 600, 342},
{11238, 20120531, 610, 11290, 20120531, 610, 238},
{11238, 20120531, 620, 11290, 20120531, 620, 438},
{11238, 20120531, 630, 11290, 20120531, 630, 483},
{11238, 20120531, 640, 11290, 20120531, 640, 498},
{11238, 20120531, 650, 11290, 20120531, 650, 535},
{11238, 20120531, 700, 11290, 20120531, 700, 527},
{11238, 20120531, 710, 11290, 20120531, 710, 461},
{11238, 20120531, 720, 11290, 20120531, 720, 572},
{11238, 20120531, 730, 11290, 20120531, 730, 624}};
(* format the time data to hourly timestamps *)
times = (
date = ToString[#2];
hour = StringPadLeft[ToString@Floor[#3/100], 2, "0"];
{date, hour}) & @@@ data;
(* join the timestamps to the data *)
datawithtimes = MapThread[Prepend, {data, times}];
(* gather the data by the timestamps *)
gathered = GatherBy[datawithtimes, First];
(* pick out the data and average *)
{StringJoin[Insert[First@#1, " ", 2], "00"],
N@Mean[#5], N@Mean[#8]} & @@ Transpose[#] & /@ gathered
{{20120530 1300, 11290., 247.667}, {20120530 1400, 11290., 238.}, {20120531 0600, 11290., 422.333}, {20120531 0700, 11290., 546.}}