Learn R Programming

SciencesPo (version 1.02.12)

tsCollapse: Aggregated Data by Time Parameters

Description

Return a data.frame with aggregated data by time parameters. In order to use this function, you have to have a as.timedf data.frame.

Usage

tsCollapse(data, by = c("day", "month", "year"), FUN, na.rm = FALSE,
  factor = NULL, civil = FALSE, plot = TRUE)

Arguments

data
a timedf object
by
the method to break up the dependent variable; options: "day","month", or "year".
FUN
a function to aggregate data sum, mean, min, max, etc.
na.rm
A logical value for na.rm, default is FALSE
factor
is the aggregator factor; a numeric value representing days, months or years to perform aggregation by.
civil
Wether the years start from the civil date or from the first observation in data. The default is civil = FALSE.
plot
whether the data.frame to be plotted, default is plot = TRUE.

encoding

UTF-8

Details

A data.frame of type long with summarized time series data (y) and time parameters formatted as POSIXct.

Examples

Run this code
data(us2012)
Obama.ts <- as.timedf(us2012[,3], '%Y-%m-%d', us2012[,8])

# Daily aggregated means for data:
daily <- tsCollapse(Obama.ts, by = "day", mean)

# Weekly aggregated means for data:
weekly <- tsCollapse(Obama.ts, by = "day", factor = 7, mean)

# monthly aggregated means for data:
monthly <- tsCollapse(Obama.ts, by = "month", mean)

# bimonthly or semimonthly aggregated means for data:
bimonthly <- tsCollapse(Obama.ts,by = "month", mean, factor = 2)

Run the code above in your browser using DataLab