averageIncome: averageIncome

View source: R/averageIncome.R

averageIncomeR Documentation

averageIncome

Description

This function calculates average income for a range of years prior to a date. The function expects a data.table with yearly incomes from which to calcu- late the average.

The function can include multiple occurrences of a patient ID, but output is restricted to one average income calculated for each combination of person and year.

If the input data includes income from many years not relevant to the project, then it is wise to trim these data prior to using the function.

Usage

averageIncome(data,income,datvars,incomevars,numyears=5)

Arguments

data

- A dataframe/table with with an identification variable and a date from which the previous years' income should be averaged.

income

- A dataframe/table with at least three variables which are the identification of individual, the year and the income

datvars

- A character vector of two: identification and the date representing the year that is just after the years used to calculate average income.

incomevars

- A character with three variables: identification variable, year and income - in that order

numyears

- the number of years to include in the average income, default is 5.

Details

The function can in particular be used with the income data provided by Statistics Denmark. These files include a record per year and sometimes a record by quarter. The function will assume that repeated incomes for a year should be removed leaving a single income per year. The actual income selected for averaging is chosen by the user when selecting variables.

Value

a data.table with 2 records, the identification variable and the averaged income.

Author(s)

Christian Torp-Pedersen

Examples

require(data.table)
dat <- data.table(pnr=c(1,2,3,3), dato=as.Date(paste0(2018:2021,"-11-11")))
indkomst <- data.table(pnr=rep(c(1,2,3,3),5),year=rep(2015:2020,5),
income=rep(200:205,5))
averageIncome(dat,indkomst,c("pnr","dato"),c("pnr","year","income"))

tagteam/heaven documentation built on April 26, 2024, 6:22 a.m.