# R Baby Height Predictor

Not surprisingly, parents are interested in knowing what their future children will be like. One topic of interest is how tall their child will be as an adult. As you might expect, height is somewhat predictable. You can predict a child’s adult height with the parents’ heights and the baby’s gender. See http://www.mayoclinic.org/child-growth/expert-answers/faq-20057990 for more detail.

Once the baby is born you can do more accurate predictions using lookup tables but that’s beyond the scope of this post.

```# What gender is the baby? Enter M or F.
gender <- "M"```
```# How tall is the mother in inches? Enter a number.
mother <- 64.5```
```# How tall is the father in inches? Enter a number.
father <- 70```
```# Use an if statement to create a correction of 5 if gender is male and
# -5 if gender is -5
if(gender=="M") correction <- 5 else
correction <- -5```
```# Calculate height by adding the mother's height, the father's height and
# the correction factor
height <- (mother + father + correction) / 2```
```# Create values that the user will understand
feet <- trunc(height / 12)
inches <- round(height - feet * 12, 0)```
```# Let the user know how tall their baby will be
print(paste("Your baby will be about ",feet," feet, ",inches," inches tall as an adult.",sep=""))

 "Your baby will be about 5 feet, 10 inches tall as an adult."```

# Calculate Pi in R

R has a built in function for pi but for pi day (3/14) I thought it would be fun to program R to calculate pi. I found a formula called Nilicantha’s formula:

# pi = 3 + 4/(2*3*4) – 4/(4*5*6) + 4/(6*7*8) – 4/(8*9*10) + 4/(10*11*12) – (4/(12*13*14) …

Here’s the code I wrote:

```options(digits = 22) # 22 is max allowed
pi_calc <- 3
x <- 2
n <- 10000```
```for(i in 1:n) {
y <- x + 1
z <- x + 2
combine_with_pi_calc <- 4 / (x * y * z)
test <- i %% 2 == 0
if (isTRUE(test)) {
pi_calc <- pi_calc - combine_with_pi_calc
}
else {
pi_calc <- pi_calc + combine_with_pi_calc
}```
```x <- z
print(pi_calc)
}```
```# Compare to the pi constant included in R
# Matches out to the 12th digit
# I think the error is due to floating point, not the formula, but I'm not sure.
print(pi)```

It works!

``` 3.141592653589537764702
 3.141592653590038697331
 3.141592653589537764702
 3.141592653590038253242
 3.141592653589537764702
 3.141592653590038253242
 3.141592653589538208792
 3.141592653590038253242
 3.141592653589538208792
>
> # Compare to the pi constant included in R
> # Matches out to the 12th digit
> print(pi)
 3.141592653589793115998```

What do you think? Has anyone else written code to calculate pi in R?