简体   繁体   中英

Split vector intro more vectors with constrains

Im working on a mealplan and I need some advice on how to split the Daily intake into several meals.

I have a vector where each element represents # of grams of different foods that a person should eat in order to meet his/her Daily intake of protein, carbs, fat, fiber..etc.

The vector looks as such:

intake <-  Salmon                 Chicken                   Pesto                  Butter 
         162.573720               96.262731               48.415283                4.560707 
            Yoghurt                Couscous              Boiled Egg                   Apple 
         190.233090              518.741711              198.049714                0.000000 
            Avocado                Broccoli              Grapefruit Quark / Kesella Natural 
           0.000000              350.000000                0.000000              217.450103 
            Oatmeal 
          67.820707 

This has lenght 13. I also have a matrix X with all the nutritional facts about each food type, it looks like this:

        Salmon Chicken   Pesto Butter Yoghurt Couscous Boiled Egg  Apple Avocado Broccoli Grapefruit
protein   0.18   0.231 0.12740  0.004  0.0342   0.0379     0.1225 0.0000  0.0194   0.0350      0.007
fat       0.12   0.010 0.53850  0.820  0.0300   0.0016     0.0973 0.0005  0.1960   0.0027      0.001
carbs     0.00   0.000 0.04080  0.005  0.0484   0.2182     0.0040 0.1058  0.0170   0.0312      0.070
fibre     0.00   0.000 0.00218  0.000  0.0000   0.0140     0.0000 0.0232  0.0475   0.0310      0.019
Greens    0.00   0.000 0.00000  0.000  0.0000   0.0000     0.0000 0.0000  1.0000   1.0000      0.000
        Quark / Kesella Natural Oatmeal
protein                   0.127   0.133
fat                       0.001   0.070
carbs                     0.036   0.576
fibre                     0.000   0.100
Greens                    0.000   0.000

Lastly, I have (0/1)- vectors that indicate if the food source is considered a "breakfast" or not. The same goes for "lunch", "dinner" and "snack".

I want to split the vector "intake" into meals that one eats throughout the day. My first idea was to simply take the dot product of intake*breakfast to get all the Foods that are considered breakfast and just eat them. This method does not give very reasonable answers since It may give way to much food to eat for breakfast and almost nothing left for the rest of the meals.

So lets say someone eats 5 meals a day;

I want to split the intake vector into 5 separate vectors, with the Foods that are considered breakfast to go in the new breakfast-vector. Also, I want to be able to adjust how much of every food that goes in each vector: such as

Default (Breakfast 50% Protein or lower, 10% Fat or higher, 40% Carbohydrates or lower, Evening Snack 70% Protein or lower, 25% Fat or higher, 5% Carbohydrates or lower).

So that I can adjust so that one eats a lot of protein for breakfast and a lot of carbs Before they work out.

When you say 50% protein, I'm not sure how you want us to quantify. How do you weight nutritional protein values against non protein foods? 50% or less protein means how much chicken relative to broccoli? I don't know much about nutrition.

Also, I had to retype up all your data so here this is for people who try to tackle this/when I come back after some clarification. Use dput() on your datasets and copy and paste the output in an edit to make your work easier for us to use.

Foods <- c("Salmon", "Chicken","Pesto","Butter","Yoghurt","Couscous","Boiled Egg","Apple","Avocado","Broccoli","Grapefruit","Quark / Kesella Natural","Oatmeal")

intake <- c(162.573720, 96.262731, 48.415283, 4.560707, 190.233090, 518.741711, 198.049714, 0.00000, 0.0000, 350.00000, 0.00000, 217.450103, 67.820707)
names(intake) <- Foods
intake

nutrition <- data.frame(protein = c(0.18, 0.231, 0.12740, 0.004, 0.0342, 0.0379, 0.1225, 0.0000, 0.0194, 0.0350, 0.007, 0.127, 0.133), fat = c(0.12, 0.010, 0.53850, 0.820, 0.0300, 0.0016, 0.0973, 0.0005, 0.1960, 0.0027, 0.001, 0.001, 0.070), carbs = c(0.00,0.000, 0.04080, 0.005, 0.0484, 0.2182, 0.0040, 0.1058, 0.0170, 0.0312, 0.070, 0.036, 0.576), fibre = c(0, 0, 0.0218, 0, 0, 0.0140, 0, 0.0232, 0.0475, 0.0310, 0.019, 0.0, 0.1), greens = c(0,0,0,0,0,0,0,0,1,1,0,0,0), row.names = Foods)
nutrition

# constraints
#Breakfast 
#protein*.5 # maximum
#fat*.1 # minimum
#carbs*.4 # maximum

#Evening Snack 
#protein*.7 # maximum
#fat*.25 # minimum
#carbs*.5 #maximum

Breakfast <- c(0,1,0,0,1,0,1,1,1,0,1,1,1)
Lunch <- c(1,1,1,1,0,1,0,0,0,1,1,1,1)
Dinner <- c(1,1,0,0,0,1,0,0,1,0,0,1,0)
meals <- cbind(Breakfast, Lunch, Dinner)

# even split amongst meals
apply(meals, 2, function(x) 
  intake[which(x == 1)] / rowSums(split(cbind(intake, nutrition, meals), x)$`1`[,colnames(meals)]))

Now the hard part you say is how to do a non-even split based on nutrition/meal food relative portions. Whats the grams to serving ratio so we know how much protein the person gets? Seems like we need that to make intake work with the nutrition information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM