![]() ![]() On the one hand, we are going to create a new categorical variable named cat_var. For this purpose, there exist three options: aggregating more than one categorical variable, aggregating multiple numerical variables or both at the same time. Hence, you can calculate the quantiles 5% and 95% for the returns of each month typing: dat <- aggregate(tserie ~ month(index(tserie)), FUN = quantile,įinally, it is worth to mention that it is possible to aggregate more than one variable. In this scenario, you may be interested in aggregating the quantiles by date (aggregate daily data to monthly or to weekly, for instance). Consider the following sample object that represents the monthly returns of an investment fund over a year: set.seed(1)ĭates <- seq(dmy(""), dmy(""), by = "day") In this section we are going to use a time series object of class xts as an example, although you could use a data frame instead to apply the function. Using aggregate in R is very simple and it is worth to mention that you can apply any function you want, even a custom function. In the following sections we will show examples and use cases about aggregating data, like aggregating the mean, the count or the quantiles, among other examples. Recall to type help(aggregate) or ?aggregate for additional information. ) # Additional arguments to be passed to FUN Ts.eps = getOption("ts.eps"), # Tolerance to determine if 'nfrequency' is a submultiple of the frequency of x Ndeltat = 1, # Fraction between successive observations ![]() Nfrequency = 1, # Observations per unit of time (submultiple of x)įUN = sum, # Function to be applied for summary statistics Na.action = na.omit) # How to deal with NA values Subset, # Observations to be used (optional) Simplify = TRUE, # Whether to simplify results as much as possible or notĭrop = TRUE) # Whether to drop unused combinations of grouping values or not.ĭata, # List or data frame where the variables are stored , # Additional arguments to be passed to FUN The arguments and its description for each method are summarized in the following block: # Data frameīy, # List of variables (grouping elements)įUN, # Function to be applied for summary statistics There are three possible input types: a data frame, a formula and a time series object. The syntax of the R aggregate function will depend on the input data. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |