I have data as follows:
library(data.table)
set.seed(1)
year = c(rep(2000,5), rep(2001,5), rep(2002,5), rep(2003,5), rep(2004,5))
DT <- data.table(panelID = sample(10,10),
some_type = as.factor(sample(0:5, 6)),
some_other_type = as.factor(sample(0:5, 6)),
Group = c(rep(1,20),rep(2,20),rep(3,20),rep(4,20),rep(5,20)),
wt = 15*round(runif(100)/10,2),
Income = round(rnorm(10,-5,5),2),
Income_proxy = round(rnorm(10,-6,6),2),
year = rep(year,4),
Happiness = sample(10,10),
Sex = round(rnorm(10,0.75,0.3),2),
Age = sample(100,100),
Height= 150*round(rnorm(10,0.75,0.3),2))
I am trying to write a function that automatically creates certain calculations, just by providing the grouping variables.
calulate_relative_dev <- function(DT, varA="Income", varB="Income_proxy", groups, years=NULL) {
if (is.null(years)) {
out_names <- paste0("rel_deviation_", groups[i])
for (i in seq_along(groups)) {
setDT(DT)[, (out_names[i]) := 100*mean((varA - varB) / varA), by=eval(groups[i])]
}
} else if (!is.null(years))
out_names <- paste0("rel_deviation_", groups[i], years[i])
for (i in seq_along(groups)) {
for (j in seq_along(years)) {
setDT(DT)[, (out_names[i]) := 100*mean((varA - varB) / varA), by=eval(groups[i], years[i])]
}
}
}
In order to do:
calulate_relative_dev(DT, groups = c("Group","some_type"))
and
calulate_relative_dev(DT, groups = c("Group","some_type"), years=year))
But when I do, I get the following error:
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 'mean': object 'Income' not found
Called from: h(simpleError(msg, call))
If I try to put Income in quotes, I get:
Error in h(simpleError(msg, call)) :
error in evaluating the argument 'x' in selecting a method for function 'mean': non-numeric argument to binary operator
Called from: h(simpleError(msg, call))
How should I write the syntax here?
years? Why not supply"year"as one of thegroups?