# One Sample T-Test

I want to test a hypothesis, where i want to test the marks scored by Boys and girls.

Gender = 1 is for males and gender = 0 is for females.

assuming I am reading a data from a CSV file this is how I am representing it.

hp<-read.csv("hyp.csv")

t.test(hp$Math[hp$Gender==1],mu=mean(hp$Math[hp$Gender==0]),alternative ="greater" ))

Is this the correct representation of the code. I am putting the girls mean in mu and boys as X.

Any help will be appreciated.

Thanks and regards,

Aditya

• You have two groups, so why do you want to use one sample t-test? – Tim Aug 28 '16 at 19:37

## 2 Answers

Try the formula interface to t.test like so:

t.test(hp$Math ~ hp$Gender, alternative = "greater")


this will perform a Welch corrected t-test for independent groups. However, it is almost never justified to test alternative = "greater". In most instances by far, you should test alternative="two.sided" which is standard for t.test, if you do not specify alternative.

The formula interface (using ~) is shorter and therefore more readable and less error prone whenever the data comes in this form.

Use mu = if you want to perform a one sample test, e. g. compare female's grade to a fixed value like 2.5.

Finally: People at CrossValidated recognize questions on R syntax as off Topic (R Syntax is not statistics). Consider asking questions like this one on StackOverflow.

There are two issues here, one about t-test and another about coding in R.

You don't have one sample from one population: you have two samples from two populations, and you are testing if means of both populations are equal. Therefore, you have to perform a two sample t-test.

About coding, you are using "hp" as variable, but "hp" is a data frame, and you need a single variable, that is one column of hp.

If scores are in a column named "score" code should be as following:

The code should be:

t.test(x=hp$score[Gender==1],y=hp$score[Gender==0],alt="greater")