I am trying to write a function my_heights() that takes a vector-like heights and returns a numeric vector with the heights in a consistent unit.
The unit should be specified as a function argument, with the default being cm. For the sake of simplicity, assume the unit can only be m or cm.
I have the following vector:
heights <- c("188 cm", "1.73 m", "192 cm", "175 cm", "165 cm", "191 cm", "1.85 m")
Desired output (sanity check):
my_heights(heights)
[1] 188 173 192 175 165 191 185
my_heights(heights, "m")
[1] 1.88 1.73 1.92 1.75 1.65 1.91 1.85
I understand that when the default function is called it would return the heights with cm as it is but the heights with m are multiplied by 100. And for the latter heights with m are returned as it is but the heights with cm are divided by 100. Not sure how should I separate the heights by cm and m to make it work correctly.