1

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)
Erdem Akca
  • 41
  • 5
  • 1
    You should change the order like `t.test(PU6_exercise_data1$memory_score ~ PU6_exercise_data1$age , var.equal = TRUE)` or a better way is `t.test(memory_score~ age, var.equal = TRUE,data = PU6_exercise_data1)` – maydin Nov 16 '21 at 14:04

1 Answers1

2

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.

VadulRashkov
  • 121
  • 3
  • Thank you for the answer, I forgot to tell you that the dataset contains 100 observations, the age variable has "young", "middle", and "old", I want to perform a t-test to see if younger people have a higher memory score better than middle or older people – Erdem Akca Nov 16 '21 at 15:48
  • 1
    You can define a variable "notyoung" that is 1 if the observation is "middle" or "old" and then the same approach would work. I am not sure if you were asking for that, but I hope it helps. – VadulRashkov Nov 16 '21 at 20:51