I have a lot of booking data (millions of rows) and want to calculate changes (differences = subtraction) of booking amounts between the same groups of different years that are stored in two separate data tables.

I can do this using the great data.table as shown in the code below but **how could the code be optimized (regarding performance and memory consumption)** since I am coyping data (tables) and have several calculation steps that could be done at once possibly?

```
# Calculate value differences for the same group of data in two different data.tables
cur <- data.table(company=c("A", "B", "New"), booking.date=seq(from=as.Date("2011/01/01"), by="week", length.out=12), sales.amount = 201:212, vat.amount = 11:22)
cur

prev <- data.table(company=c("A", "B"), booking.date=seq(from=as.Date("2010/01/01"), by="month", length.out=10), sales.amount = 101:110, vat.amount = 1:10)
prev

diff <- copy(prev)   # copy to keep the original data.table unchanged
diff[, `:=`(sales.amount = -sales.amount, vat.amount = -vat.amount)]   # negate the amounts so that the sum will be the difference
diff <- rbind(diff, cur)  # combine negative previous amounts with positive current amounts so that the sum will be difference
diff  # show raw data
diff[, .(last.booking.date=max(booking.date), sales.amount.diff=sum(sales.amount), vat.amount.diff=sum(vat.amount)), by=company] # calculate the difference

# Look at company "A" to verify the result:
cur[company=="A",]
prev[company=="A",]

```

The example data and the expected output look like this:

Data table 1: Current year's bookings:

```
> cur
    company booking.date sales.amount vat.amount
 1:       A   2011-01-01          201         11
 2:       B   2011-01-08          202         12
 3:     New   2011-01-15          203         13
 4:       A   2011-01-22          204         14
 5:       B   2011-01-29          205         15
 6:     New   2011-02-05          206         16
 7:       A   2011-02-12          207         17
 8:       B   2011-02-19          208         18
 9:     New   2011-02-26          209         19
10:       A   2011-03-05          210         20
11:       B   2011-03-12          211         21
12:     New   2011-03-19          212         22

```

Data table 2: Previous year's bookings:

```
> prev
   company booking.date sales.amount vat.amount
 1:       A   2010-01-01          101          1
 2:       B   2010-02-01          102          2
 3:       A   2010-03-01          103          3
 4:       B   2010-04-01          104          4
 5:       A   2010-05-01          105          5
 6:       B   2010-06-01          106          6
 7:       A   2010-07-01          107          7
 8:       B   2010-08-01          108          8
 9:       A   2010-09-01          109          9
10:       B   2010-10-01          110         10

```

**Expected result** (difference per company of the sums of each booking year):

```
   company last.booking.date sales.amount.diff vat.amount.diff
1:     A 1        2011-03-05               297              37
2:     B 1        2011-03-12               296              36
3:   New 1        2011-03-19               830              70

```