Import packages

library(plyr) # loading plyr for mutate function


Creating new variables using mutate

Calculating Body Mass Index (BMI) from height and weight from women dataset

# converting weight from pounds to kilogram
women_BMI <- mutate(women, weight_kg = weight / 2.2) # weight_kg = weight in kilograms

# converting height from inches to meters 
women_BMI <- mutate(women_BMI, height_m = height * 0.0254) # height_m = height in meters

# calculating BMI from weight_kg and height_m
women_BMI <- mutate(women_BMI, BMI = weight_kg / (height_m * height_m)) 

# validating BMI calculation
head(women_BMI)
##   height weight weight_kg height_m      BMI
## 1     58    115  52.27273   1.4732 24.08528
## 2     59    117  53.18182   1.4986 23.68055
## 3     60    120  54.54545   1.5240 23.48490
## 4     61    123  55.90909   1.5494 23.28924
## 5     62    126  57.27273   1.5748 23.09389
## 6     63    129  58.63636   1.6002 22.89910