汇总每日内容 [英] Aggregating daily content

查看:86
本文介绍了汇总每日内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试汇总(有些不稳定)的每日数据。我实际上正在使用csv数据,但是如果我重新创建它-看起来像这样:

I've been attempting to aggregate (some what erratic) daily data. I'm actually working with csv data, but if i recreate it - it would look something like this:

library(zoo)

dates <- c("20100505", "20100505", "20100506", "20100507")
val1 <- c("10", "11", "1", "6")
val2 <- c("5", "31", "2", "7")

x <- data.frame(dates = dates, val1=val1, val2=val2)
z <- read.zoo(x, format = "%Y%m%d")

现在,我想每天汇总一次(请注意,有时候一天中有> 1个数据点,有时还没有。)

Now i'd like to aggregate this on a daily basis (notice that some times there are >1 datapoint for a day, and sometimes there arent.

我尝试了很多变化,但似乎无法汇总,因此例如失败了:

I've tried lots and lots of variations, but i cant seem to aggregate, so for instance this fails:

aggregate(z, as.Date(time(z)), sum)
# Error in Summary.factor(2:3, na.rm = FALSE) : sum not meaningful for factors

关于聚合似乎有很多内容,我尝试了很多版本,但似乎无法每天总结一下除了每日汇总之外,还希望计算cummax和累计平均值。

There seems to be a lot of content regarding aggregate, and i've tried a number of versions but cant seem to sum this on a daily level. I'd also like to run cummax and cumulative averages in addition to the daily summing.

任何帮助将不胜感激。

更新

Update

我实际使用的代码如下:

The code I am actually using is as follows:

z <- read.zoo(file = "data.csv", sep = ",", header = TRUE, stringsAsFactors = FALSE, blank.lines.skip = T, na.strings="NA", format = "%Y%m%d");

我上面的数字(无意间)的引用似乎与实际中的相似,因为当我这样做时:

It seems my (unintentional) quotation of the numbers above is similar to what is happening in practice, because when I do:

aggregate(z, index(z), sum)
#Error in Summary.factor(25L, na.rm = FALSE) : sum not meaningful for factors

没有意义等),我如何将它们指定为as.numeric自动? ( stringAsFactors = False 似乎不起作用?)

There a number of columns (100 or so), how can i specify them to be as.numeric automatically ? (stringAsFactors = False doesnt appear to work?)

推荐答案

或者您在使用Zoo之前进行汇总(val1和val2需要为数字)。

Or you aggregate before using zoo (val1 and val2 need to be numeric though).

x <- data.frame(dates = dates, val1=as.numeric(val1), val2=as.numeric(val2))
y <- aggregate(x[,2:3],by=list(x[,1]),FUN=sum)

然后将 y 喂入动物园。

请避免该警告:)

这篇关于汇总每日内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆