Skip to content

Commit 81f08cb

Browse files
committed
list variation of the example
1 parent 68e7d80 commit 81f08cb

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

Examples/OrderedGrouping/OrderedGrouping.Rmd

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ transform <- blocks_to_rowrecs_spec(
5151
controlTable = diagram,
5252
recordKeys = 'ID')
5353
54+
# a function to paste a vector of strings together
5455
concat_values = function(v) {
5556
paste(sort(unique(v)), collapse=", ")
5657
}
@@ -241,6 +242,36 @@ res <- d %.>% ops
241242
knitr::kable(res)
242243
```
243244

245+
We could also try this with an alternate concatenation that builds lists (instead of concatenating strings).
246+
247+
```{r}
248+
249+
# a function to collect values in a sorted list
250+
concat_values2 = function(v) {
251+
list(sort(unique(v)))
252+
}
253+
254+
255+
# specify the operations
256+
ops2 <- local_td(d) %.>%
257+
project(., # fuse all the ops on same date/id into one string
258+
OP := concat_values2(OP),
259+
groupby = c("ID", "DATE")) %.>%
260+
extend(., # rank each ID group in order of date
261+
rank %:=% row_number(),
262+
partitionby = "ID",
263+
orderby = "DATE") %.>%
264+
transform %.>% # transform the record shape
265+
orderby(., # ensure presentation is ordered by ID
266+
'ID')
267+
268+
# apply the operations to data
269+
res2 <- d %.>% ops2
270+
271+
# present the results
272+
knitr::kable(res2)
273+
```
274+
244275
And we are done.
245276

246277
## A variation

Examples/OrderedGrouping/OrderedGrouping.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
325364
And we are done.
326365

327366
## A variation

0 commit comments

Comments
 (0)