-6

enter image description hereI have data for customer purchases across different products , I calculated the amount_spent by multiplying Item Numbers by the respective Price

I used cut function to segregate people into different age bins, Now how can I find the aggregate amount spent by different age groups i.e the contribution of each age group in terms of dollars spent

Please let me know if you need anymore info

I am really sorry that I can't paste the data here due to remote desktop constraints . I am actually concerned with the result I got after summarize function

enter image description here

av abhishiek
  • 647
  • 2
  • 11
  • 26
  • 3
    what have you tried so far, also dont post images of your data but `dput` it – mtoto Jan 21 '16 at 17:57
  • @mtoto This data is located on a remote server , I can't copy it , allow mw to post screenshot of my code with the result – av abhishiek Jan 21 '16 at 18:08
  • you don't need to paste your exact code. You can create a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) of what you are trying to do. – user5249203 Jan 21 '16 at 19:42
  • If I understood what you are trying to do is something similar to [This SO question](http://stackoverflow.com/questions/15047742/sum-of-rows-based-on-column-value). – user5249203 Jan 21 '16 at 19:45

1 Answers1

1
library(dplyr)
customer_transaction %>% group_by(age_gr) %>% select(amount_spent) %>% summarise_each(funs(sum))

Though I am not sure if you want the contribution to the whole pie or just the sum in each age group.

If your data is of class data.table you could go with

customer_transaction[,sum(amount_spent),by=age_gr] 
Martin Schmelzer
  • 23,283
  • 6
  • 73
  • 98
  • when I try the second method, I am getting "unused argument by = age_gr " error , I want the sum of amount spen for each age group exampe 25-30 3500$ ,30-40 4000 $ , ,would the output of your first and second query be same ? – av abhishiek Jan 21 '16 at 19:22
  • Yes they would be. If you declared your data to be `data.table[customer_transaction]` then the second one should work actually. – Martin Schmelzer Jan 21 '16 at 19:26
  • Sorry, I'm mobile right now. I of course meant `data.table(customer_transaction)` – Martin Schmelzer Jan 21 '16 at 19:35