Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the R equivalent of pandas .resample() method?

Tags:

python

pandas

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.

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!

like image 728
Alex Petralia Avatar asked Jul 24 '15 17:07

Alex Petralia


1 Answers

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")]

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.

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.

-Jeffrey Ryan from xts manual.

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)

like image 100
Dean MacGregor Avatar answered Oct 12 '22 06:10

Dean MacGregor