简体   繁体   English

分割向量介绍更多有约束的向量

[英]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: 长度为13。我还有一个矩阵X,其中包含每种食物类型的所有营养成分,它看起来像这样:

        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. 最后,我有(0/1)-向量,指示食物来源是否被视为“早餐”。 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. 我想将摄入量向量分为5个单独的向量,将被视为早餐的食物放入新的早餐向量中。 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). 默认值(早餐50%蛋白质或更低,10%脂肪或更高,40%碳水化合物或更低,晚间小吃70%蛋白质或更低,25%脂肪或更高,5%碳水化合物或更低)。

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. 当您说50%的蛋白质时,我不确定您希望我们如何量化。 How do you weight nutritional protein values against non protein foods? 您如何权衡非蛋白质食品的营养蛋白质价值? 50% or less protein means how much chicken relative to broccoli? 50%或更少的蛋白质意味着鸡肉相对于西兰花多少钱? 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. 在数据集上使用dput(),然后将输出复制并粘贴到编辑中,以使您的工作更易于我们使用。

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. 似乎我们需要它来使摄入量与营养信息一起起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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