Perform independent samples t-test

Viewed 80

I have a dataset that looks like this

Id           age       memory_score
1            young     71.11172
2            old       67.65542 
3            young     65.34716 
4            young     81.21345 
5            old       80.72106 
6            old       73.01981 

I want to do an independent T-test to test the hypothesis that younger people have a higher memory score than middle-aged or older people.

This is the code that I tried, but unfortunately it gives me an error called: grouping factor must have exactly 2 levels. I want to know how I can resolve this.

t.test(PU6_exercise_data1$age ~ PU6_exercise_data1$memory_score, var.equal = TRUE)
1 Answers

This can be fixed by changing the order of the formula.

Here is some code to recreate your data.

id <- 1:6
age <- c("young", "old", "young", "young", "old", "old")
memory_score <- c(71.11172, 67.65542, 65.34716, 81.21345, 80.72106, 73.0198)
df <- data.frame(id = id, age = age, memory_score = memory_score)
  

Then, we can perform the t-test:

t.test(df$memory_score ~ df$age, var.equal = TRUE)

You can see this in the examples of the formula interface in the documentation for t.test.

Related