I'm having a trouble trying to apply the t Student test for two samples in the data of the in the image below :table content The diff column is the difference of the observations. The experiment is:

"A field ecologist is interested in examining the effect of sewage disposal in the diversity of invertebrates normally found in rivers. She dispatches a team of graduate students to count how many different species can be found in 100-Liter samples of water from seven rivers that have untreated sewage disposal at some point in their course, from both 100m before and after the point where the sewage is disposed. For each river, 10 samples are collected (one each month, from february to november)."

To create the table I've used:

diff = dados2$Upstream - dados2$Downstream
diff <-data.frame(diff)
grupo <- data.frame(dados1$River, diff, mes)
agg <-aggregate(diff~dados1.River:mes, data = grupo, FUN = mean)

I'm trying to use : t.test(diff~mes, data = agg) but don't work in this way. I received the following message: grouping factor must have exactly 2 levels

How I can execute this test (two sample and paired) for the content of the table (the complete table have 70 rows and nine months)?

  • 2
    $\begingroup$ Although as a purely programming problem this belongs on Stack Overflow, we might keep it here on CV to the extent you are open to discussing your statistical strategy, because it is problematic. In particular, given that there are seven rivers to compare, why have you chosen a t-test and how do you propose to apply it? Separately from this consideration, have you considered the prospect that the samples might exhibit strong temporal correlation? $\endgroup$
    – whuber
    Apr 10, 2015 at 16:35

1 Answer 1


The problem is you don't have 2 samples. You have 9 samples (9 months). You can't get a single t-statistic for that. Instead you can get 8 t-statistics from contrasts between months (well, you can get more, but for a single saturated linear model, you can get 8).

Additionally, your samples are not independent, you collected samples from the same rivers each month. Assuming what you want to know is the effect of months on your difference score, this is a classic repeated measures ANOVA design. You tried to handle the dependence issue by averaging over the interaction of month and river. Have you looked at your aggregated data? It should be identical to your original data, because each river is measured at each month once, so the the interaction is of length 1 (i.e. the mean of River C at Month April is just the single value you have for River C at Month April).

To do a within groups ANOVA, this will give you the omnibus test: summary(aov(diff ~ mes + Error(dados1.River/mes), data=d1)). That is probably not very relevant. Instead, what you probably want is the linear effect (as you go from Feb-November does the difference increase or decrease linearly). To do that you need to create linear combination of your diff scores by month for each river. One set of linear contrast codes for your 10 months would be (-4.5,-3.5,-2.5,-1.5,-0.5,0.5,1.5,2.5,3.5,4.5). For River A you would take the February data, and multiple it by -4.5. Then you would take the March data for River A and multiply it by -3.5, etc. until you multiple the November score for River A by 4.5. Then you add up all those values. Do that same for River B (you can get R to do this either using aggregate() or reshaping the data into wide form and doing a simple arithmetic operation on the 10 month columns that would be formed. If you are interested in quadratic trends or any other form you think the data may take you can look into how to construct contrasts that represent your desired form.

Once you have the data structured with a column of Rivers and a column of contrast scores (those values you just computed in the previous paragraph) you do a simple linear model testing the contrast score against the intercept; e.g.: summary(lm(diff.contrast~1,data=dat))

  • $\begingroup$ Thank you for reply. I researched about some "topics" that you've mentioned and solved my problem. I am very grateful! $\endgroup$
    – Fabricio
    Apr 10, 2015 at 17:45
  • $\begingroup$ You're welcome. If you happen to come back here, please mark this as an answer if you think it suffices. $\endgroup$
    – le_andrew
    Apr 10, 2015 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.