Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accumulate values for every possible combination in R

Let's say I have data test (dput given) where a list-col say items:

test <- structure(list(items = list('a', c('b', 'c'), c('d', 'e'), 'f', c('g', 'h')),
               ID = c(1,1,1,2,2)), row.names = c(NA, 5L), class = "data.frame")

library(tidyverse)
test %>% group_by(ID) %>%
  mutate(dummy = accumulate(items, ~paste(.x, .y)))

I am getting an output with list-col like this

  items ID        dummy
1     a  1            a
2  b, c  1     a b, a c
3  d, e  1 a b d, a c e
4     f  2            f
5  g, h  2     f g, f h

I would like there to be four items in row3, having each possible combination, i.e. c("a b d", "a b e", "a c d", "a c e"). It however doesn't matter if these are separate items in the list or not. In other words, the output of dummy may be of type multi-level list, where row3 will contain four items in the list. I tried using expand.grid, but I am doing something wrong somewhere!

So my desired output will look like

  items ID                      dummy
1     a  1                          a
2  b, c  1                   a b, a c
3  d, e  1 a b d, a c d, a b e, a c e
4     f  2                          f
5  g, h  2                   f g, f h
like image 312
AnilGoyal Avatar asked May 16 '21 13:05

AnilGoyal


People also ask

How do you make all combinations of a vector in R?

To create combination of multiple vectors, we can use expand. grid function. For example, if we have six vectors say x, y, z, a, b, and c then the combination of vectors can be created by using the command expand. grid(x,y,z,a,b,c).


1 Answers

Another approach with expand.grid(),

test %>% group_by(ID) %>%
mutate(dummy = accumulate(items, ~do.call("paste",expand.grid(.x, .y)))) %>% 
data.frame()

gives,

  items ID                      dummy
1     a  1                          a
2  b, c  1                   a b, a c
3  d, e  1 a b d, a c d, a b e, a c e
4     f  2                          f
5  g, h  2                   f g, f h
like image 63
maydin Avatar answered Oct 21 '22 00:10

maydin