Calculate t-statistic step by step in R Hello: I'm a faculty member teaching a stats class, and I'd like to show students how to calculate the t-statistic from an independent samples t-test (mostly) step-by-step. Using the code below, though I get a slightly different t-value. Can anyone see what I'm doing wrong? 
#load dataset
library(reshape2)
data(tips)

#create tip percent variable
tips$percent=tips$tip/tips$total_bill

#Split dataset for ease
splits<-split(tips, tips$sex)

#Save data sets
women<-splits[[1]] 
men<-splits[[2]]

#variance by group sample size
var_women<-var(women$percent)/length(women$percent)
var_men<-var(men$percent/length(men$percent))

#Sum
total_variance<-var_women+var_men

#Squre Root
sqrt_variance<-sqrt(total_variance)

#Group means by pooled variances
(mean(women$percent)-mean(men$percent))/sqrt_variance
#T.test
t.test(percent~sex, data=tips)

 A: Your data set is probably not the best illustrative example in terms of normality assumption... but anyway, here is some quick R code to reproduce some of the calculation of t.test().
Equal variances 
t.test(percent ~ sex, data=tips, var.equal=TRUE)
# Two Sample t-test
# 
# data:  percent by sex
# t = 1.0834, df = 242, p-value = 0.2797
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
#   -0.007232898  0.024913277
# sample estimates:
#   mean in group Female   mean in group Male 
# 0.1664907            0.1576505 

x1 <- tips$percent[tips$sex == "Female"]
x2 <- tips$percent[tips$sex == "Male"]
n1 <- length(x1)
n2 <- length(x2)

var.pooled <- weighted.mean(x=c(var(x1), var(x2)), w=c(n1 - 1, n2 - 1))
t <- (mean(x1) - mean(x2)) / sqrt(var.pooled / n1 + var.pooled / n2)
t
# [1] 1.083397
df <- n1 + n2 - 2
df
# [1] 242

$$
$$
Unequal variances
t.test(percent ~ sex, data=tips, var.equal=FALSE)
# Welch Two Sample t-test
# 
# data:  percent by sex
# t = 1.1433, df = 206.759, p-value = 0.2542
# alternative hypothesis: true difference in means is not equal to 0
# 95 percent confidence interval:
#   -0.006404119  0.024084498
# sample estimates:
#   mean in group Female   mean in group Male 
# 0.1664907            0.1576505 

x1 <- tips$percent[tips$sex == "Female"]
x2 <- tips$percent[tips$sex == "Male"]
n1 <- length(x1)
n2 <- length(x2)

t <- (mean(x1) - mean(x2)) / sqrt(var(x1) / n1 + var(x2) / n2)
t
# [1] 1.143277
df.num <- (var(x1) / n1 + var(x2) / n2)^2
df.denom <- var(x1)^2 / (n1^2 * (n1 - 1)) + var(x2)^2 / (n2^2 * (n2 - 1))
df <- df.num / df.denom
df
# [1] 206.7587

A: Just change the braces from
var_men<-var(men$percent/length(men$percent))
to
var_men<-var(men$percent)/length(men$percent).
