-1

I have a data frame DF:

         DF=data.frame(index=1:6,pos=I(list(c(21, 22, 24),c(21, 24),c(21, 22, 23, 24), 26, c(1, 21),c(14, 21, 23))))

the data structure:

str(DF)

'data.frame':   6 obs. of  2 variables:
 $ index: int  1 2 3 4 5 6
 $ pos  :List of 6
  ..$ : int  21 22 24
  ..$ : int  21 24
  ..$ : int  21 22 23 24
  ..$ : int 26
  ..$ : int  1 21
  ..$ : int  14 21 23

I would like to generate a column to hold unique elements by combining the first row with the following rows, and still as a list. So the expected result is:

         index            pos       result
           1     21, 22, 24         21, 22, 24
           2         21, 24         21, 22, 24
           3 21, 22, 23, 24     21, 22, 23, 24
           4             26     21, 22, 24, 26
           5          1, 21      1, 21, 22, 24
           6     14, 21, 23     14, 21, 22, 23, 24

The first row in "result" column is the same as "pos", the second row is the list of unique elements by combining the first row and the second row of "pos" column, ... I expect the mode and class of column "result" is the same as "pos". Appreciate any helps for this.

user3354212
  • 1,048
  • 8
  • 19
  • `Reduce(union, DF$pos, accumulate=TRUE)` probably. Next time, post reproducible code for the example, please. – Frank Sep 11 '17 at 14:22
  • The result is not what I expected. the row 5 and row 6 are not right. – user3354212 Sep 11 '17 at 14:30
  • Yeah, some variation on it may be required, but that's up to you since you did not give reproducible code here. You can see the documentation at `?Reduce` if you think it might work; I'm just guessing. – Frank Sep 11 '17 at 14:31
  • 1
    I didn't know how to read in the data frame with a list column, I found how to do it [https://stackoverflow.com/questions/9547518/create-a-data-frame-where-a-column-is-a-list] – user3354212 Sep 11 '17 at 14:51
  • 2
    `Map(union, DF$pos[1], DF$pos)` – talat Sep 11 '17 at 14:54
  • Another way of building the example is `DF2 = data.frame(id = 1:6); DF2$pos = list(c(21, 22, 24),c(21, 24),c(21, 22, 23, 24), 26, c(1, 21),c(14, 21, 23))` ... admittedly, R demands weird workarounds for this. – Frank Sep 11 '17 at 14:57
  • 1
    You can always just dput() the data. – csgroen Sep 11 '17 at 15:00

1 Answers1

2

You want to apply a function (union) to each element of your list-column and the first element of the same list-column. That's what Map is made for:

Map(union, DF$pos[1], DF$pos)
# [[1]]
# [1] 21 22 24
# 
# [[2]]
# [1] 21 22 24
# 
# [[3]]
# [1] 21 22 24 23
# 
# [[4]]
# [1] 21 22 24 26
# 
# [[5]]
# [1] 21 22 24  1
# 
# [[6]]
# [1] 21 22 24 14 23

If you want to sort the resulting elements and assign them to a new column, you can use:

DF$result <- Map(function(...) sort(union(...)), DF$pos[1], DF$pos)
talat
  • 68,970
  • 21
  • 126
  • 157