Aggregate: Aggregate numeric, Date and categorical variables

Description Usage Arguments Value Author(s) Examples

Description

The Aggregate function (not to be confounded with aggregate) prepares a data frame or data table for merging by computing the sum, mean and variance of all continuous (integer and numeric) variables by a given variable. For all categorical variabes (character and factor), it creates dummies and subsequently computes the sum and the mode by a given variable. For all Date variables, it computes the recency and duration by a given variable with repsect the an end date variable. For computational speed, all the calculations are done with data.table. This functions aims at maximum information extraction with a minimum amount of code.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Aggregate(
  x,
  by,
  end_ind = Sys.Date(),
  format = "%Y-%m-%d",
  tibble = FALSE,
  verbose = TRUE,
  object = NULL,
  p = "all"
)

Arguments

x

A data frame or data table. Categorical variables have to be of type character or factor and continuous variables have to be of type integer or numeric. Date variables should be in the Date format.

by

A character string specifying the variable on which to aggregate the results. Note that 'by' should be a variable of the table 'x'.

end_ind

A Date object, or something which can be coerced by as.Date(origin, ...) to such an object. If not specified, we take the Sys.Date() as end date.

format

A character string. If not specified, the ISO 8601 international standard which expresses a day "%Y-%m-%d" is taken.

tibble

Should the output be a tibble, data frame or data table? By default, the function returns a data frame or data table depending on the input. To return a tibble, the user must set the tibble = TRUE.

verbose

indicator Used to show the progress.

object

Parameter related to the dummy function. See ?dummy for more information.

p

Parameter related to the dummy function. See ?dummy for more information.

Value

A data frame, data table or tibble with the aforementioned variables aggregated by the given ID variables. If the input is a data frame, a data frame is returned else a data table is returned.

Author(s)

Authors: Matthias Bogaert, Michel Ballings, Dirk Van den Poel, Maintainer: matthias.bogaert@UGent.be

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# Example
# Create some data
data <- data.frame(V1=sample(as.factor(c('yes','no')), 200000, TRUE),
                  V2=sample(as.character(c(1,2,3,4,5)),200000, TRUE),
                  V3=sample(1:20000,200000, TRUE),
                  V4=sample(300:1000, 200000, TRUE),
                  V5 = sample(as.Date(as.Date('2014-12-09'):Sys.Date()-1,
                  origin = "1970-01-01"),200000,TRUE),
                  ID=sample(x = as.character(1:4), size = 200000, replace = TRUE))

Aggregate(x=data,by='ID')

# Examples of how to use the object and p argument. See dummy and categories function for details.
# Aggregate(x=data,by='ID',object=categories(data))
# Aggregate(x=data,by='ID',p=2)

Example output

Calculating categorical variables ... 
Calculating numerical variables ... 
Calculating date variables ... 
  ID V1_no_sum V1_no_mode V1_yes_sum V1_yes_mode V2_1_sum V2_1_mode V2_2_sum
1  1     25240          1      24835           0     9896         0    10044
2  2     25126          1      24780           0     9856         0     9886
3  3     25175          1      24897           0    10226         0     9831
4  4     24992          1      24955           0     9969         0    10062
  V2_2_mode V2_3_sum V2_3_mode V2_4_sum V2_4_mode V2_5_sum V2_5_mode    V3_sum
1         0    10147         0    10185         0     9803         0 502783363
2         0     9823         0    10222         0    10119         0 499835933
3         0    10207         0     9913         0     9895         0 501327453
4         0     9894         0     9921         0    10101         0 498838536
    V3_mean   V3_var   V4_sum  V4_mean   V4_var V5_duration V5_recency
1 10040.606 33333844 32547476 649.9746 41096.82        2186          1
2 10015.548 33507525 32549623 652.2186 41202.50        2186          1
3 10012.132 33401164 32550580 650.0755 40982.54        2186          1
4  9987.357 33350478 32385032 648.3879 40978.60        2186          1

AggregateR documentation built on Nov. 20, 2020, 5:07 p.m.