我正在尝试使用R通过读取我的CSV文件来绘制数据,该文件包含一些每秒记录的值。我希望R将数据聚合到每分钟,以便我可以使用plot(TIME,VALUE.)
绘制每分钟数据。
我的CSV文件包含类似的内容;
Store No.,Date,Time,Watt
33,2011/09/26,09:11:01,0.0599E+03
34,2011/09/26,09:11:02,0.0597E+03
35,2011/09/26,09:11:03,0.0598E+03
36,2011/09/26,09:11:04,0.0596E+03
37,2011/09/26,09:11:05,0.0593E+03
38,2011/09/26,09:11:06,0.0595E+03
39,2011/09/26,09:11:07,0.0595E+03
40,2011/09/26,09:11:08,0.0595E+03
41,2011/09/26,09:11:09,0.0591E+03
我无法按分钟汇总时间和瓦特列,因为我是R的新手。任何帮助都将受到高度赞赏。
答案 0 :(得分:5)
假设商店号无关紧要,并且将问题中显示的示例数据中的最后三行更改为09:12:..
而不是09:11:..
,因此我们至少有两个不同的分钟:
# create test data
Lines <- "Store No.,Date,Time,Watt
33,2011/09/26,09:11:01,0.0599E+03
34,2011/09/26,09:11:02,0.0597E+03
35,2011/09/26,09:11:03,0.0598E+03
36,2011/09/26,09:11:04,0.0596E+03
37,2011/09/26,09:11:05,0.0593E+03
38,2011/09/26,09:11:06,0.0595E+03
39,2011/09/26,09:12:07,0.0595E+03
40,2011/09/26,09:12:08,0.0595E+03
41,2011/09/26,09:12:09,0.0591E+03"
cat(Lines, "\n", file = "data.txt")
# read in aggregating at the same time
library(zoo)
library(chron)
z <- read.zoo("data.txt", header = TRUE, sep = ",", index = 2:3,
FUN = paste, FUN2 = function(x) trunc(as.chron(x), "00:01:00"),
aggregate = mean)[, -1]
此处FUN
适用于index
指定的列。它会将它们粘贴在一起,然后将FUN2
应用于FUN
创建chron
日期/时间的结果。最后,使用FUN2
的平均值汇总具有相同Watt
值的行:
> z
(09/26/11 09:11:00) (09/26/11 09:12:00)
59.63333 59.36667
根据需要,aggregate
参数可以更改为aggregate = function(x) tail(x, 1)
,而不是显示的aggregate
参数。
有关更多信息和示例,请加载zoo包并查看?read.zoo
,?aggregate.zoo
和vignette("zoo-read")
以及其他插图和帮助文件。
更新:使用FUN2
参数进行轻微简化。不确定但是read.zoo
参数
在第一次回答这个问题时可能不存在。