0

I have a dataframe containing sells by day, but as there are many values for one day, so i'd like to rebuilt my dataframe to have just one value per day which is the sum of all the sells of this day.

what i have:

what i have

what i'd like to have:

enter what i'd like to have here

(the values are not well calculated but it's for the example)

I tried agregate and functions like this but it dosn't work and I dont know how to do this...

Thanks for help

  • 1
    Yeah it's a duplicate but it's very easy with data.table : `setDT(df)[, tot_by_day = sum(TOT_OP_MAIN_PAID_MNT), by = DATE_ID]` – Mbr Mbr Jun 23 '17 at 13:26
  • setDT(df)[, tot_by_day = sum(TOT_OP_MAIN_PAID_MNT), by = DATE_ID] Error in `[.data.table`(setDT(df), , tot_by_day = sum(TOT_OP_MAIN_PAID_MNT), : unused argument (tot_by_day = sum(TOT_OP_MAIN_PAID_MNT)) – Martin Delisle Jun 23 '17 at 13:52
  • Your command should work but I dont understand why I have this error – Martin Delisle Jun 23 '17 at 13:55
  • It's probably due to the name of your columns. Check if the syntax is correct + don't forget to install the package and load if it's not the case by writing : `install.packages("data.table")` then `require(data.table)` – Mbr Mbr Jun 23 '17 at 13:56
  • Now i have this, I changed types and I tested your command and I get this setDT(df)[, sum(TOT_OP_MAIN_PAID_MNT), by=DATE_ID] DATE_ID V1 1: NA And now my dates are all NA in my df – Martin Delisle Jun 23 '17 at 14:16

2 Answers2

1

Aggregate should work

aggregate(TOT_OP_MAIN_PAID_MNT,by=list(DATE_ID),FUN=sum)
Mr. Bugle
  • 325
  • 2
  • 12
1

This should work

df <- data.frame(DATE_ID = 1, TOT_OP_MAIN_PAID_MNT = 1)
aggregate(TOT_OP_MAIN_PAID_MNT ~ DATE_ID, df, sum)
Dirk Nachbar
  • 542
  • 4
  • 16