You can refer the following:
Suppose these are your data sets:
data1 <- data.frame(n = 15, mean = 14, sd = 8)
data2 <- data.frame(n = c(17, 5, 8), mean = c(19, 17, 11), sd = c(7, 6, 9))
T.test <- function(n, mean, sd) {
s <- sum((n - 1) * sd^2) / (sum(n) - 2) # weighted variance
t <- sqrt(prod(n) / sum(n)) * (diff(mean) / sqrt(s)) # t statistic
df <- sum(n) - 2 # degrees of freedom
p <- (1 - pt(abs(t), df)) * 2 # p value
c(t = t, p = p)
}
Apply the function to all rows of data2:
apply(data2, 1, function(x) T.test(c(x[1], data1$n),
c(x[2], data1$mean),
c(x[3], data1$sd)))
The output shows t- and p-values:
[,1] [,2] [,3]
t -1.98618371 -0.8215838 0.8730255
p 0.05621594 0.4220631 0.3925227
Hope this helps!
To learn more, enroll with R programming for data science online.
Thanks.