Performing t test in R programming

+1 vote

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

Feb 12, 2019 in Data Analytics by Tyrion anex
• 8,700 points
686 views

1 answer to this question.

0 votes

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.

answered Feb 12, 2019 by Sophie may
• 10,620 points

Related Questions In Data Analytics

0 votes
2 answers

What are the rules to define a variable name in R programming language?

The same rules almost follow for all ...READ MORE

answered Aug 26, 2019 in Data Analytics by anonymous
• 33,030 points
15,111 views
+1 vote
1 answer

Difference between factor and as.factor in R programming

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

answered Oct 29, 2018 in Data Analytics by Maverick
• 10,840 points
4,858 views
0 votes
1 answer

What is active binding in R programming

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

answered Oct 30, 2018 in Data Analytics by Maverick
• 10,840 points
1,462 views
+1 vote
1 answer

Remove NA values from the output in R programming

Edit your code: columnmean <- function(x, removeNA = ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 52,350 points
875 views
+10 votes
3 answers

Which is a better initiative to learn data science: Python or R?

Well it truly depends on your requirement, If ...READ MORE

answered Aug 9, 2018 in Data Analytics by Abhi
• 3,720 points
1,613 views
+1 vote
1 answer

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

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

answered Nov 15, 2018 in Data Analytics by Maverick
• 10,840 points
4,815 views
+1 vote
2 answers
0 votes
1 answer

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

answered Nov 15, 2018 in Data Analytics by Maverick
• 10,840 points
799 views
0 votes
1 answer

Can we have an if loop inside a for loop in R programming?

You're If loop doesn't have any condition ...READ MORE

answered Dec 21, 2018 in Data Analytics by Sophie may
• 10,620 points
683 views
0 votes
1 answer

R Programming error in 'fert'

You're using a factor: fert <- factor(c(50,20,10,10,20,50)) levels(fert) #[1] ...READ MORE

answered Dec 28, 2018 in Data Analytics by Sophie may
• 10,620 points
716 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP