Retrieve all possible combinations of n items of a given size k and apply the function sum to another column


xxxvincxxx

I have a df that looks like:

  item value
1    a     1
2    b     4
3    c     3
4    d     2
5    e     6
6    f     8
7    g    11

df <- data.frame(stringsAsFactors=FALSE,
        item = c("a", "b", "c", "d", "e", "f", "g"),
       value = c(1L, 4L, 3L, 2L, 6L, 8L, 11L))

I want to generate all possible combinations of size = 3, for example:

size <- 3

combo_3 <- combn(df$item, size, simplify = F)

Now I want to summarize this result.

I want a dataframe with the following contents:

  • Portfolio Index
  • Items in the portfolio
  • the sum of the column values ​​for that particular combination

Here is an example data frame where the first combination appears:

combo_index    item    sum_total
1                 a        8
1                 b        8
1                 c        8
2                 a        7
2                 b        7
2                 d        7
3                 a        11
3                 b        11
3                 e        11 
             ...
             ...
             ...
ice cream toucan

Same as @akrun, but use map_dfrinstead rbindlistandlapply

library(tidyverse)

map_dfr(
  combo_3, 
  ~ data.frame(item = .x, sum_total = sum(df$value[df$item %in% .x])),
  .id = 'combo_index')

#     combo_index item sum_total
# 1             1    a         8
# 2             1    b         8
# 3             1    c         8
# 4             2    a         7
# 5             2    b         7
# 6             2    d         7
# 7             3    a        11
# 8             3    b        11
# 9             3    e        11
# 10            4    a        13
# ...

Related


all possible combinations of k with lists of size n

Theodore Narliyski: I want to get all possible combinations of size K from a list of size N. I have a list with "person" objects and I am trying to create a new ArrayList which will be filled with the list of objects. Each table will be a different combination

all possible combinations of k with lists of size n

Theodore Narliyski: I want to get all possible combinations of size K from a list of size N. I have a list with "person" objects and I am trying to create a new ArrayList which will be filled with the list of objects. Each table will be a different combination

R: Create all possible combinations of a given column

Sakib Rahman | I have a data corresponding to df. df shows the source and destination and the longitude and latitude of this source and destination. I want to generate df1 with df. df1 gives all possible combinations of source and destination, combined with th

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

Find all combinations of size at least k to n

Vietnam's I am struggling to figure out the formula to solve this problem: Given an array of nnumbers and a limit k, count all distinct combinations of at least size .k E.g:A=[1,2,3] k = 2 output = 4 // [1,2],[1,3],[1,2,3],[2,3] The array can contain repeated

all possible combinations of items in n lists

Bernat Ibanez I need to develop a list that contains all possible combinations in order of elements in n lists. Basically, I'm trying to find all possible paths that will be needed later in another part of the program. I've written some simple code for the two

all possible combinations of items in n lists

Bernat Ibanez I need to develop a list that contains all possible combinations in order of elements in n lists. Basically, I'm trying to find all possible paths that will be needed later in another part of the program. I've written some simple code for the two

All possible combinations of N objects in K buckets

Aliresa Norri Say I have 3 boxes labeled A, B, C and I have 2 balls, B1 and B2. I want to get all possible combinations of these balls in a box. Note that it is important to know the balls in each box, which means that B1 and B2 are not the same. A B

k possible number combinations of size each sorted by sum

Moxf Given a set of nnumbers; what code is used to generate all possible -size subsets kin descending order (reducing the sum of each value)? example: Set={9,8,6,2,1}=> n=5and k=3. So the output is: [9,8,6] [9,8,2] [9,8,1] [9,6,2] [9,6,1] [8,6,2] [8,6,1] [9,2

Find all possible combinations of numbers to reach a given sum

James P. How would you test all possible combinations of additions of a given set of Nnumbers in order to add them up to a given final number? A simple example: A set of numbers to add:N = {1,5,22,15,0,...} Desired result:12345 Manuel Salvadores This problem c