Below is an example of what I want to do. eval(substitute(*))
works well, as shown here, but makes the code a bit harder to read. I was wondering if there is anything better I am not aware of.
I want to be able to choose the row and column variables of the table (at the end). So, if I have
input.col <- 'Gender'
input.row <- 'Region'
I want to be able to pass these arguments to the data table instead of Region
and Gender
being used as below.
library(data.table)
library(reshape)
set.seed(5)
DT <- data.table(Region = sample(x = c('Asia', 'Americas', 'Africa', 'Europe', 'Oceania'), size = 200, replace = T), Weight = runif(n = 200, min = 1, max = 5), Age = round(x = 10*rexp(n = 200, rate = 1), digits = 0), Gender = sample(x = c('Male', 'Female', 'Gender diverse'), size = 200, replace = T, prob = c(0.49, 0.49, 0.02)))
cast(data = DT[, sum(Weight), .(Region, Gender)], formula = Region~Gender, fun.aggregate = sum, value = 'V1')
I want to get to the following table
Region Female Gender diverse Male
1 Africa 32.95019 3.222125 77.50863
2 Americas 49.12787 0.000000 84.97214
3 Asia 41.04879 0.000000 55.43294
4 Europe 45.39469 4.296767 47.76714
5 Oceania 65.89198 1.439075 72.27496
Thank you!