I'm a programming novice trying to calculate some ideal body weight numbers from a large dataset of height, sex, and actual body weight. I would like to create a new column in the data frame (df$ibw) based on the ideal body weight calculation for each individual.
Ideal body weight (IBW) is calculated differently for men and women.
For males... IBW = 50 + 0.91((Height in cm)-152.4)
For females... IBW = 45.5 + 0.91((Height in cm)-152.4)
set.seed(1000)
weight <- rnorm(10, 100, 20) # weight in kilograms
sex <- (0:1) # 0 for Male, 1 for Female
height <- rnorm(10, 150, 10) # height in centimeters
df <- data.frame(weight, sex, height)
df
I've been reading other posts using if else
statements and other conditional formats, but I keep getting errors. This is something I will be frequently doing for datasets and I'm trying to figure out the best way to accomplish this task.
df$IBW <- 0.91 * (df$height - 152.4) + 50 - 4.5 * df$sex
? – etienne