2

I have a data.table dt with the following structure:

country calendar_date                                                     net_revenue
 US    2018-05-17                                3.5,28.0, 3.5, 3.5,10.5, 5.6,...
 US    2018-05-18                           3.5,102.9,229.6,  8.4,  3.5,  2.1,...
 US    2018-05-19                                3.5,13.3,35.0, 7.0,52.5, 3.5,...
 US    2018-05-20                          3.85, 7.00,58.10, 7.00, 3.50, 7.00,...
 US    2018-05-21                               17.5, 3.5, 3.5,10.5, 1.4, 3.5,...
 US    2018-05-22        5.60000, 3.50000,17.50000, 3.50000, 2.10000, 7.05516,...
 US    2018-05-23       17.50000,16.10000,58.01507, 2.80000, 5.60000, 3.50000,...
 US    2018-05-24        3.50000,26.72765, 3.50000,12.60000, 3.50000, 3.50000,...
 US    2018-05-25                           2.1,308.0,  2.1,  2.1,  3.5,  3.5,...
 US    2018-05-26        2.10000, 3.50000,88.90000, 3.50000, 3.50000, 7.75859,...
 US    2018-05-27        5.22087,17.50000, 5.60000, 3.50000, 7.00000, 7.00000,...
 US    2018-05-28                                3.5,35.0, 1.4, 3.5, 7.0,28.0,...
 US    2018-05-29                                9.1, 7.0,23.1, 1.4, 1.4, 9.1,...
 US    2018-05-30                                7.7, 2.1,10.5,15.4,65.1, 3.5,...

Where the column net_revenue is a nested list:

str(dt)
Classes ‘data.table’ and 'data.frame':  14 obs. of  3 variables:
 $ country      : chr  "US" "US" "US" "US" ...
 $ calendar_date: chr  "2018-05-17" "2018-05-18" "2018-05-19" "2018-05-20" ...
 $ net_revenue  :List of 14
  ..$ : num  3.5 28 3.5 3.5 10.5 5.6 14 2.1 3.5 28 ...
  ..$ : num  3.5 102.9 229.6 8.4 3.5 ...
  ..$ : num  3.5 13.3 35 7 52.5 3.5 7 35 3.5 19.6 ...
  ..$ : num  3.85 7 58.1 7 3.5 7 1.4 3.5 34.3 2.1 ...
  ..$ : num  17.5 3.5 3.5 10.5 1.4 3.5 15.4 26.6 10.5 5.6 ...
  ..$ : num  5.6 3.5 17.5 3.5 2.1 ...
  ..$ : num  17.5 16.1 58 2.8 5.6 ...
  ..$ : num  3.5 26.7 3.5 12.6 3.5 ...
  ..$ : num  2.1 308 2.1 2.1 3.5 ...
  ..$ : num  2.1 3.5 88.9 3.5 3.5 ...
  ..$ : num  5.22 17.5 5.6 3.5 7 ...
  ..$ : num  3.5 35 1.4 3.5 7 28 3.5 3.5 3.5 7 ...
  ..$ : num  9.1 7 23.1 1.4 1.4 9.1 7 2.1 5.6 2.1 ...
  ..$ : num  7.7 2.1 10.5 15.4 65.1 3.5 28 3.5 24.5 19.6 ...
 - attr(*, ".internal.selfref")=<externalptr>

What I need to do is create a column net_revenue_roll that concatenates the net_revenue lists of the 7 last dates for each calendar_date by country - i.e. "rolling(ly)" apply a function (either list or c) to a nested list in a data.table by group.

Referring to similar questions (links at the end), I tried 3 approaches so far, but none of them seem to work correctly for this specific problem:

### Option 1 - rollapplyr
dt[, net_revenue_roll := zoo::rollapplyr (net_revenue, 7L, list), by = c('country')]

### Option 2 - lapply + .SD
dt[, net_revenue_roll := lapply (.SD, function (x) {list (shift(x, 0L:6L, type = 'lag'))}), by = c('country'), .SDcols = c('net_revenue')]

### Option 3 - Reduce + .SD
dt[, net_revenue_roll := Reduce (list, shift(.SD, 0L:6L, type = 'lag')), by = c('country'), .SDcols = c('net_revenue')]

I suspect that I am making some wrong assumption about the order in which functions are being applied, but I can't find the error. Any suggestions?

Links: Rolling by group in data.table R How do I take a rolling product using data.table

zx8754
  • 52,746
  • 12
  • 114
  • 209
  • so you want last 7 values from each list for each date and country. that's the question? – YOLO Jul 20 '18 at 18:57
  • Hi, @YOLO! Nope, I want to concatenate the 7 last lists for each date grouping by country. I edited the question to make it clearer :) – Pietro Franzero Jul 20 '18 at 19:03
  • can you show the structure of the nested list, just the first row – YOLO Jul 20 '18 at 19:04
  • @YOLO sure... It is a list with a numeric vector: `[[1]] [1] 3.50000 28.00000 3.50000 3.50000 10.50000 5.60000 14.00000 2.10000 3.50000 28.00000 10.50000 3.42755 7.00000 [14] 2.10000 7.00000 7.00000 8.40000 3.50000 10.50000 11.90000 28.00000 3.50000 3.50000 1.40000 3.50000 4.90000 [27] 14.00000` – Pietro Franzero Jul 20 '18 at 19:32
  • EDIT: I added a chunk of code with `str(dt)` to clarify the structure of the data.table – Pietro Franzero Jul 20 '18 at 19:36

1 Answers1

2

Using DT from the Note at the end convert each list element to a character string, use rollapplyr and convert back.

library(data.table)
library(zoo)

DT[, ch := sapply(net_revenue, toString)][,
  ch := rollapplyr(ch, 7, toString, partial = TRUE), by = "country"][,
  net_revenue := lapply(strsplit(ch, ","), type.convert)][,
  ch:=NULL]

Note

The input in reproducible form is:

library(data.table)

Lines <- "
country calendar_date                                             net_revenue
 US    2018-05-17                                3.5,28.0, 3.5, 3.5,10.5, 5.6
 US    2018-05-18                           3.5,102.9,229.6,  8.4,  3.5,  2.1
 US    2018-05-19                                3.5,13.3,35.0, 7.0,52.5, 3.5
 US    2018-05-20                          3.85, 7.00,58.10, 7.00, 3.50, 7.00
 US    2018-05-21                               17.5, 3.5, 3.5,10.5, 1.4, 3.5
 US    2018-05-22        5.60000, 3.50000,17.50000, 3.50000, 2.10000, 7.05516
 US    2018-05-23       17.50000,16.10000,58.01507, 2.80000, 5.60000, 3.50000
 US    2018-05-24        3.50000,26.72765, 3.50000,12.60000, 3.50000, 3.50000
 US    2018-05-25                           2.1,308.0,  2.1,  2.1,  3.5,  3.5
 US    2018-05-26        2.10000, 3.50000,88.90000, 3.50000, 3.50000, 7.75859
 US    2018-05-27        5.22087,17.50000, 5.60000, 3.50000, 7.00000, 7.00000
 US    2018-05-28                                3.5,35.0, 1.4, 3.5, 7.0,28.0
 US    2018-05-29                                9.1, 7.0,23.1, 1.4, 1.4, 9.1
 US    2018-05-30                                7.7, 2.1,10.5,15.4,65.1, 3.5"
L <- trimws(readLines(textConnection(Lines)))
L <- sub("\\s+", ";", L)
L <- sub("\\s+", ";", L)
DF <- read.table(text = L, header = TRUE, sep = ";", as.is = TRUE)
DF$net_revenue <- lapply(strsplit(DF$net_revenue, ","), type.convert)
DT <- as.data.table(DF)
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Hi, @g-grothendieck! Thank you for the answer. It was a clever idea to convert to string - it works indeed. However, this script is going on production, so ideally I'd like something more efficient. I'm going to upvote your answer now, but I'm not closing the question yet - I want to see if someone can give this more efficient answer. Cheers – Pietro Franzero Jul 21 '18 at 19:01