I am trying to convert an R data.table code snippet into an appropriate function, but I am not having success.
I would like to summarize a variable using this code:
library(data.table)
mtcars_dt <-
data.table(mtcars)
# Expression
mtcars_dt[,list(.N),by=cyl][order(cyl),list(cyl,N,Proportion=N/sum(N))]
The result is a data.table (as intended):
cyl N Proportion
1: 4 11 0.34375
2: 6 7 0.21875
3: 8 14 0.43750
Then I wish to create a function and apply this function across multiple variables, with a data.table resulting from each iteration of the loop applying the function:
# turn into function and apply loop, returning DT from each iteration
var_list <- c('cyl','gear')
for (i in var_list){
# generalize the code above
}
I am not sure the best way to proceed. I tried this solution, but I am losing the variable name in the first column. I wonder if I am on the wrong track with eval(quote(...))
# My attempt, not working yet!
var_list <- c(quote(cyl),quote(gear))
f_numeric_cat <-
function(dt,var1) {
dt[,list(.N),by=eval(var1)][order(eval(var1)),Proportion:=N/sum(N)][]
}
for (i in var_list){
print(f_numeric_cat(mtcars_dt,i))
}
var1 N Proportion
1: 6 7 0.21875
2: 4 11 0.34375
3: 8 14 0.43750
var1 N Proportion
1: 4 12 0.37500
2: 3 15 0.46875
3: 5 5 0.15625