pandas .resample()方法的R等效项是什么? [英] What is the R equivalent of pandas .resample() method?

查看:63
本文介绍了pandas .resample()方法的R等效项是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我找到的最接近的链接:

This is the closest link I've found: https://stats.stackexchange.com/questions/5305/how-to-re-sample-an-xts-time-series-in-r

但是我没有看到关于在熊猫中可以汇总数据的不同方式(例如均值,计数,匿名函数)的任何信息.

But I don't see anything about the different ways to aggregate the data (like mean, count, anonymous function) which you can do in pandas.

对于我的程序,我试图让一个数据帧每2分钟重新采样一次,并取每个间隔的2个值的平均值.谢谢!

For my program, I'm trying to have a dataframe be resampled every 2 minutes and take the average of the 2 values at each interval. Thanks!

推荐答案

如果您使用data.tablelubridate,它可能看起来像这样

If you use data.table and lubridate it might look something like this

library(data.table)
library(lubridate)
#sample data
dt<-data.table(ts=seq(from=ymd('2015-01-01'), to=ymd('2015-07-01'),by='mins'), datum=runif(260641,0,100))

如果您希望从分钟到每小时获取数据,则可以这样做

if you wanted to get the data from minute to hourly means you could do

 dt[,mean(datum),by=floor_date(ts,"hour")]

如果您有一堆列,并且希望将它们平均化,则可以这样做

if you had a bunch of columns and you wanted all of them to be averaged you could do

dt[,lapply(.SD,mean),by=floor_date(ts,"hour")]

您可以将mean替换为所需的任何功能.您可以将小时"替换为秒",分钟",小时",天",周",月",年".好吧,您不能从几分钟到几秒钟,因为那需要魔术,但无论如何您可以从微秒到几秒.

You can replace mean for any function you'd like. You can replace "hour" with "second", "minute", "hour", "day", "week", "month", "year". Well you can't go from minute to seconds as that would require magic but you can go from micro seconds to seconds anyway.

不可能将序列从较低的周期转换为 较高的周期性-例如每周至每天或每天至5分钟吧, 因为那需要魔法.

It is not possible to convert a series from a lower periodicity to a higher periodicity - e.g. weekly to daily or daily to 5 minute bars, as that would require magic.

-来自xts手册的Jeffrey Ryan.

-Jeffrey Ryan from xts manual.

我从没学过xts,所以我不知道使用xts对象的语法,但是那行很著名(或者至少和手册中的那行一样著名)

I never learned xts so I don't know the syntax to do it with xts objects but that line is famous (or at least as famous as a line from a manual can be)

这篇关于pandas .resample()方法的R等效项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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