0

I have a data.table like the following:

x <- data.table(group = c('A', 'A', 'A', 'B', 'B'),
                row_id = c(1, 2, 3, 1, 2),
                value = c('a', 'b', 'c', 'd', 'e'))

I want to add a new column that cumulatively concatenate column 'value' ordered by 'row_id', within each group indicated by 'group'. So the output would look like:

   group row_id value
1:     A      1     a
2:     A      2   a_b
3:     A      3 a_b_c
4:     B      1     d
5:     B      2   d_e

Thank you for your help!

nancy
  • 1
  • 1
    Several alternatives in the link. [This](https://stackoverflow.com/a/24864073/1851712) is basically the same as posted below. – Henrik Oct 30 '18 at 16:32

1 Answers1

0

One option would be to group by 'group', loop through the sequence of rows, get the sequence of it, use that as index to get the corresponding 'value' and paste together with delimiter _, assign (:=) it to update the 'value'

x[, value := sapply(seq_len(.N), function(i) 
     paste(value[seq(i)], collapse = "_")), by = group]
x
#   group row_id value
#1:     A      1     a
#2:     A      2   a_b
#3:     A      3 a_b_c
#4:     B      1     d
#5:     B      2   d_e
akrun
  • 874,273
  • 37
  • 540
  • 662