@@ -322,6 +322,45 @@ knitr::kable(res)
322322| 5 | 2003-11-09 00:00:00 | B | 2010-10-10 00:00:00 | A | NA | NA |
323323| 6 | 2004-01-09 00:00:00 | B | NA | NA | NA | NA |
324324
325+ We could also try this with an alternate concatenation that builds lists
326+ (instead of concatenating strings).
327+
328+ ``` r
329+ # a function to collect values in a sorted list
330+ concat_values2 = function (v ) {
331+ list (sort(unique(v )))
332+ }
333+
334+
335+ # specify the operations
336+ ops2 <- local_td(d ) %. > %
337+ project(. , # fuse all the ops on same date/id into one string
338+ OP : = concat_values2(OP ),
339+ groupby = c(" ID" , " DATE" )) %. > %
340+ extend(. , # rank each ID group in order of date
341+ rank %: = % row_number(),
342+ partitionby = " ID" ,
343+ orderby = " DATE" ) %. > %
344+ transform %. > % # transform the record shape
345+ orderby(. , # ensure presentation is ordered by ID
346+ ' ID' )
347+
348+ # apply the operations to data
349+ res2 <- d %. > % ops2
350+
351+ # present the results
352+ knitr :: kable(res2 )
353+ ```
354+
355+ | ID | DATE1 | OP1 | DATE2 | OP2 | DATE3 | OP3 |
356+ | -: | :------------------ | :-- | :------------------ | :---------- | :------------------ | :-- |
357+ | 1 | 2001-01-02 00:00:00 | A | 2015-04-25 00:00:00 | B | NA | NA |
358+ | 2 | 2000-04-01 00:00:00 | A | NA | NA | NA | NA |
359+ | 3 | 2014-04-07 00:00:00 | D | NA | NA | NA | NA |
360+ | 4 | 2005-06-16 00:00:00 | A | 2009-01-20 00:00:00 | c(“B”, “D”) | 2012-12-01 00:00:00 | C |
361+ | 5 | 2003-11-09 00:00:00 | B | 2010-10-10 00:00:00 | A | NA | NA |
362+ | 6 | 2004-01-09 00:00:00 | B | NA | NA | NA | NA |
363+
325364And we are done.
326365
327366## A variation
0 commit comments