Reputation: 1611
I have a data frame with two columns: LOOKUP
which contains an index number, and the other, VALUE
which holds a string associated with that number:
LOOKUP VALUE
1000 Apple
100 Banana
10 Grape
1 Orange
Shown below in R code:
dat <- data.frame(LOOKUP= c(1000, 100, 10, 1),
VALUE = c("Apple", "Banana", "Grape", "Orange"))
In this example, there are 15 possible sums of the lookup value:
These sums are used to understand, for lack of a better description, what fruits are in a cart. For example, if the sum = 1100, we know that the cart has an apple and a banana. Does anybody know how I would do this for all possible combinations in my set? My desired output is a new data frame along the lines of:
SUM VALUES
1111 Apple, Banana, Grape, Orange
...
1100 Apple, Banana
...
11 Grape, Orange
1 Apple
Upvotes: 1
Views: 61
Reputation: 40171
One solution involving purrr
, tibble
, dplyr
and tidyr
could be:
map(.x = 1:nrow(dat), ~ combn(dat$VALUE, .x, paste, collapse = ",") %>%
enframe()) %>%
bind_rows() %>%
rowid_to_column() %>%
separate_rows(value) %>%
left_join(dat, by = c("value" = "VALUE")) %>%
group_by(rowid) %>%
summarise(comb = toString(value),
sum = sum(LOOKUP))
rowid comb sum
<int> <chr> <dbl>
1 1 Apple 1000
2 2 Banana 100
3 3 Grape 10
4 4 Orange 1
5 5 Apple, Banana 1100
6 6 Apple, Grape 1010
7 7 Apple, Orange 1001
8 8 Banana, Grape 110
9 9 Banana, Orange 101
10 10 Grape, Orange 11
11 11 Apple, Banana, Grape 1110
12 12 Apple, Banana, Orange 1101
13 13 Apple, Grape, Orange 1011
14 14 Banana, Grape, Orange 111
15 15 Apple, Banana, Grape, Orange 1111
Sample data:
dat <- data.frame(LOOKUP= c(1000, 100, 10, 1),
VALUE = c("Apple", "Banana", "Grape", "Orange"),
stringsAsFactors = FALSE)
Upvotes: 1