1
votes

I have a data frame in which I have one single variable and multiple factor columns. It looks like this

Species<-list("a","b")
Species<-rep(Species,times=6)
Class<-list("X","X","Y","Y","Z","Z")
Class<-rep(Class,times=2)
Sample<-list("1","1","1","1","1","1","2","2","2","2","2","2")
Treatment<-list("A","A","A","A","A","A","B","B","B","B","B","B")
values<-c(15,16,17,18,19,20,5,6,7,8,9,10)

prova<-as.data.frame(cbind(Species, Class,Treatment,Sample,values))

I would like to perform a series of t-tests to compare, for each class, the value of each Species observed at the two treatments. So I am interested in the differences between the two treatments, but if I apply the code t.test(values~Treatment, data = prova, method="t.test") it calculates differences irrespective of the other factors, which is not what I am looking for. Moreover, I am only interested in comparing the Species "a" in the class "X" at the two treatments.

In the original dataset I have three replicates for each Treatment.

Can you please help me? Thank you for any suggestion

1

1 Answers

0
votes

You need the columns in a list to be in a vector:

prova<-data.frame(
Species=as.character(Species), 
Class = as.character(Class),
Treatment = as.character(Treatment),
Sample = as.character(Treatment),
values = values)

Or if possible, have Species, Class etc as a character vector to start with. Now you can use by , which splits your data.frame according to Class and performs a t.test like you tried to do:

results = by(prova,prova$Class,function(i)t.test(values ~ Treatment,data=i))
names(results)
[1] "X" "Y" "Z"

To look at results for "X", do

results[["X"]]

If you want to only look at species "a", you can do:

prova = subset(prova,Species=="a")

and rerun the above code. Here i must note your example dataset will not work because it has n=1.