+1 vote

I want to perform t-test on two data sets. How can this be done?

Feb 12, 2019 614 views

## 1 answer to this question.

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!

Thanks.

• 10,610 points

+1 vote

## Difference between factor and as.factor in R programming

Hey @Ali, as.factor is a wrapper for ...READ MORE

## What is active binding in R programming

Active bindings in R are much like ...READ MORE

+1 vote

+1 vote

## Error saying "vector size cannot be NA" when using R with data mining

You can use the removesparseterm function.  Removes sparse ...READ MORE

+1 vote

## How to segment documents into phrases in text mining using R?

You can do this in R using ...READ MORE

## Trying to find frequent itemsets of a data set using arules package

Try replacing ID <- c("A123","A123","A123","A123","B456","B456","B456") item <- c("bread", "butter", "milk", ...READ MORE

+1 vote