Solving linear equation in a tibble?

Asked by At

Given the following equation definition:

For men: (10 x w) + (6.25 x h) - (5 x a) + 5
For women:(10 x w) + (6.25 x h) - (5 x a) - 161


w = weight in kg (1 pound = 0.45359237 kilograms)
h = height in cm (1 inch = 2.54 centimeters)
a = age (in years)

Activity Factor Category Definition:

1.2 if sedentary, little or no exercise and desk job
1.375 if lightly Active, light exercise, or sports 1-3 days a week
1.55 if moderately active, moderate exercise, or sports 3-5 days a week
1.725 if very active, hard exercise, or sports 6-7 days a week
1.9 if extremely active, hard daily exercise or sports and physical job

I am trying to build a reverse calculation given N-1 variables.

For example given:

target_calories: 1500
gender: F
weight = 50
height = 170
What is her age?

age = (target_calories - ((10*w) + (6.25*h) + 5))/(-5)

I am trying to generalize it and here is what I have done so far:

var_table <- tibble(gender = "M", 
                      w_kg = w_kg, 
                      h_cm = h_cm,
                      age = age,
                      activity_factor = activity_factor)

  var2calc <- names(which(sapply(var_table, anyNA)))

I can solve this using a lot of if-else statements but I am trying to make it efficient and elegant.

I thought about calculations within the above tibble var_table + dplyr verbs and given a missing variable value, calculate it according to a specific formula.

Please advise what is the best way to do this?

0 Answers