index_column | R Documentation |
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.
index_column(
.DF,
var_to_index,
time_var = "Year",
index_time = NULL,
indexed_var = paste0(var_to_index, suffix),
suffix = "_indexed"
)
.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 " |
index_time |
the time to which data in |
indexed_var |
the name of the indexed variable. Default is " |
suffix |
the suffix to be appended to the indexed variable. Default is " |
Note that this function works when the variable to index is a column of numbers or a column of matrices.
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
.
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.