Please advise, I have the following simple function:
mifflin_equation <- function(gender = "M",
w_kg = 50,
h_cm = 180,
age = 40,
activity_type = "sedentary") {
activity_types <- c("sedentary", "light", "moderate", "active")
if (!(tolower(activity_type) %in% activity_types)) {
activity_type <- "sedentary"
}
activity_trans_table <- tibble(type = activity_types,
activity_coeff = c(1.2, 1.375,
1.55, 1.725))
activity_coeff <- activity_trans_table$activity_coeff[activity_trans_table$type == tolower(activity_type)]
common_equation <- (10 * w_kg) + (6.25 * h_cm) - (5 * age)
if (gender == "M") {
return((common_equation + 5) * activity_coeff)
} else if (gender == "F") {
return((common_equation - 161) * activity_coeff)
}
}
I am building some options:
age <- seq.int(30,90)
h <- seq.int(150, 200)
w <- seq.int(40, 150)
activity <- c("sedentary", "light", "moderate", "active")
gender <- c("M", "F")
all_options <- expand.grid(age = age, h = h, w = w, activity = activity, gender = gender)
But when I am trying to dplyr::mutate a calculated field of the above function I get first calculation ok and all NA's:
mifflin_options <- all_options %>%
dplyr::mutate(mifflin_eq_calories = mifflin_equation(gender = gender,
w_kg = w,
h_cm = h,
age = age,
activity_type = activity))
I am aware that if it was only one variable I should use sapply
, but what is the solution here?