index_column: Index a column in a data frame by groups relative to an...

View source: R/utilities.R

index_columnR Documentation

Index a column in a data frame by groups relative to an initial year

Description

This function indexes (by ratio) variables in vars_to_index to the first time in time_var or to index_time (if specified). Groups in .DF are both respected and required. Neither var_to_index nor time_var can be in the grouping variables.

Usage

index_column(
  .DF,
  var_to_index,
  time_var = "Year",
  index_time = NULL,
  indexed_var = paste0(var_to_index, suffix),
  suffix = "_indexed"
)

Arguments

.DF

the data frame in which the variables are contained

var_to_index

the column name representing the variable to be indexed (a string)

time_var

the name of the column containing time information. Default is "Year".

index_time

the time to which data in var_to_index are indexed. If NULL (the default), index_time is set to the first time of each group.

indexed_var

the name of the indexed variable. Default is "<<var_to_index>>_<<suffix>>".

suffix

the suffix to be appended to the indexed variable. Default is "_indexed".

Details

Note that this function works when the variable to index is a column of numbers or a column of matrices.

Value

a data frame with same number of rows as .DF and the following columns: grouping variables of .DF, var_to_index, time_var, and one additional column containing indexed var_to_index named with the value of indexed_var.

Examples

library(dplyr)
library(tidyr)
DF <- data.frame(Year = c(2000, 2005, 2010), a = c(10, 15, 20), b = c(5, 5.5, 6)) %>%
  gather(key = name, value = var, a, b) %>%
  group_by(name)
index_column(DF, var_to_index = "var", time_var = "Year", suffix = "_ratioed")
index_column(DF, var_to_index = "var", time_var = "Year", indexed_var = "now.indexed")
index_column(DF, var_to_index = "var", time_var = "Year", index_time = 2005,
             indexed_var = "now.indexed")
## Not run: 
  DF %>%
    ungroup() %>%
    group_by(name, var) %>%
    index_column(var_to_index = "var", time_var = "Year") # Fails! Do not group on var_to_index.
  DF %>%
    ungroup() %>%
    group_by(name, Year) %>%
    index_column(var_to_index = "var", time_var = "Year") # Fails! Do not group on time_var.

## End(Not run)

matsindf documentation built on July 4, 2024, 1:11 a.m.