Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Function call isn't returning an output

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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
}

>Solution :

Change the last line to:

tab.genderspend[]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading