Data Frames in R

Pandas has been very successful as a tool for working with time series data. For example, to run 5 minutes, you can use the resample function as follows:

 import pandas as pd dframe = pd.read_table("test.csv", delimiter=",", index_col=0, parse_dates=True, date_parser=parse) ## 5 minutes mean dframe.resample('t', how = 'mean') ## daily mean ts.resample('D', how='mean') 

How can I accomplish this in R?

0
source share
3 answers

In R, you can use the xts package, specializing in time series manipulation. For example, you can use the period.apply function as follows:

 library(xts) zoo.data <- zoo(rnorm(31)+10,as.Date(13514:13744,origin="1970-01-01")) ep <- endpoints(zoo.data,'days') ## daily mean period.apply(zoo.data, INDEX=ep, FUN=function(x) mean(x)) 

There are several handy shells for this feature,

 apply.daily(x, FUN, ...) apply.weekly(x, FUN, ...) apply.monthly(x, FUN, ...) apply.quarterly(x, FUN, ...) apply.yearly(x, FUN, ...) 
+3
source

R has data frames (data.frame) and can also read csv files. For instance.

 dframe <- read.csv2("test.csv") 

For dates, you may need to specify columns using the colClasses parameter. See ?read.csv2 . For instance:

 dframe <- read.csv2("test.csv", colClasses=c("POSIXct",NA,NA)) 

Then you can round the date field using round or trunc , which allows you to split the data into the desired frequencies.

For instance,

 dframe$trunc.times <- trunc(dframe$date.field,1,units='mins'); means <- daply(dframe, 'trunc.times', function(df) { return( mean(df$value) ) }); 

Where value is the name of the field you want to average.

0
source

Personally, I really like the combination of lubridate and zoo aggregate () for these operations:

 ts.month.sum <- aggregate(ts.data, month, sum) ts.daily.mean <- aggregate(ts.data, day, mean) ts.mins.mean <- aggregate(ts.data, minutes, mean) 

You can also use standard timemon () or yearqtr () functions or custom functions for separation and application. This method is syntactically sweeter than the pandas method.

0
source

Source: https://habr.com/ru/post/950999/


All Articles