# t Student test for two samples (and paired) in R. How to Apply for my specific data table?

I'm having a trouble trying to apply the t Student test for two samples in the data of the in the image below : 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:

mes<-data.frame(mes)
diff = dados2$Upstream - dados2$Downstream
diff <-data.frame(diff)
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)?

• 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?
– whuber
Apr 10, 2015 at 16:35

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))