I've created a function in R that works with data.table's. It takes in three different data.table's (a monthly transactions dataset, a products dataset, and a customers dataset) and should return another data.table.
When I run the code outside of the function on the data.table's it works perfectly and the last line of code prints the data.table but when I call the function on the data.table's, it runs without error but doesn't return the data.table.
I've created a different function that returns a data.table in a similar manner to this function and it returns the table the way it should so I am not sure why this function is not returning the table.
genderspend <- function(monthtrans_dat, products_dat, customers_dat) {
require(data.table)
setDT(monthtrans_dat)
setDT(products_dat)
setDT(customers_dat)
tab.revenue <- merge(monthtrans_dat, products_dat[,c("product_id","category")], by="product_id")
tab.revenue[, revenue := price*quantity]
tab.ordercustomers <- merge(tab.revenue, customers_dat, by = "customer_id")
tab.genderspend <- tab.ordercustomers[, .(total_spend = sum(revenue)), by = "gender"]
tab.genderspend[, percentage := 100*(total_spend/sum(total_spend))]
tab.genderspend
}