Home > front end >  Producing dataframe in R with rows summing to same number, including all possible combinations of nu
Producing dataframe in R with rows summing to same number, including all possible combinations of nu

Time:12-11

I am trying to create a dataframe in R.

I have 4 categories (e.g. Blue, Red, Yellow, Green) and I would like each row to sum to 100%. For each category I want to create incrimental differences of 5% units and produce a dataframe which has all possible combinations of numbers (to nearest 5%) for the 4 categories. I realise I am not explaining this well at all, so I have tried to show what I mean in the following table:

Blue Red Yellow Green
95 5 0 0
95 0 5 0
95 0 0 5
5 95 0 0
0 95 5 0
0 95 0 5
5 0 95 0
0 5 95 0
0 0 95 5
5 0 0 95
0 5 0 95
0 0 5 95
90 10 0 0
90 0 10 0
90 0 0 10
10 90 0 0
0 90 10 0
0 90 0 10
10 0 90 0
0 10 90 0
0 0 90 10
10 0 0 90
0 10 0 90
0 0 10 90
90 5 5 0
90 5 0 5
90 0 5 5
5 90 5 0
5 90 0 5
0 90 5 5
5 5 90 0
5 0 90 5
0 5 90 5
5 5 0 90
5 0 5 90
0 5 5 90
85 15 0 0
85 0 15 0
85 0 0 15
15 85 0 0
0 85 15 0
0 85 0 15
15 0 85 0
0 15 85 0
0 0 85 15
15 0 0 85
0 15 0 85
0 0 15 85
85 10 5 0
85 10 0 5
85 5 10 0
85 0 10 5
85 5 0 10
85 0 5 10
10 85 5 0
10 85 0 5
5 85 10 0
0 85 10 5
5 85 0 10
0 85 5 10
10 5 85 0
10 0 85 5
5 10 85 0
0 10 85 5
5 0 85 10
0 5 85 10
10 5 0 85
10 0 5 85
5 10 0 85
0 10 5 85
5 0 10 85
0 5 10 85
85 5 5 5

I am struggling to know where to start here...

CodePudding user response:

You could nest three for loops and bind the results together:

target_df <- data.frame()

for (i in seq(95, 0, by = -5)) {
  for (j in seq(100 - i, 0, by = -5)) {
    for(k in seq(100 - i - j, 0, by = -5)) {
      target_df <- rbind(target_df, data.frame(Blue = i, Red = j, Yellow = k, Green = 100 - i - j - k))
    }
  }
}

This returns

    Blue Red Yellow Green
1     95   5      0     0
2     95   0      5     0
3     95   0      0     5
4     90  10      0     0
5     90   5      5     0
6     90   5      0     5
7     90   0     10     0
8     90   0      5     5
9     90   0      0    10
10    85  15      0     0

You might want to remove three rows containing 100 in columns Red, Yellow and Green.

  • Related