Pregunta

Here is an example of what my data looks like:

metabolite  treatment area
x           A         1
x           B         2
x           C         3
y           A         4
y           B         5
y           C         6
z           A         7
z           B         8
z           C         9
x           A         12
x           B         22
x           C         32
y           A         42
y           B         52
y           C         62
z           A         72
z           B         82
z           C         92

For each metabolite , I want to test whether the mean areas of the treatments are different. In other words:

Metabolite X : A vs. B, A vs. C, B vs. C Metabolite Y : A vs. B, A vs. C, B vs. C Metabolite Z : A vs. B, A vs. C, B vs. C

I need to perform Tukey's Test (http://en.wikipedia.org/wiki/Tukey%27s_range_test) in R, which will make each pairwise comparison of the treatments (A vs. B, A vs. C, and C vs. B). Here is what I've come up with, but it does not work.

`for (i in levels(data$metabolite)) { 
tukey_part1 <- aov(data$area ~ data$treatment)
tukey_part2 <- TukeyHSD(x=tukey_part1, 'data$treatment', conf.level=0.95) 
                             } `

Help, please!

¿Fue útil?

Solución

Well, you never actually made an attempt to use the i variable that you are looping over. I went ahead and changed the for loop to use lapply instead. How about this

res<-lapply(levels(data$metabolite), function(i) {
    met<-data[data$metabolite==i, ]  #subset for metabolite i
    part1<-with(met, aov(area ~ treatment))
    print(part1)
    list( 
        part1 = part1,
        part2 = TukeyHSD(x=part1, 'treatment', conf.level=0.95)
    )
})
names(res)<-levels(data$metabolite)

This will give you a list or lists that contain the parts you're looking for. For example you can extract results with res[[_metabolite_]][[_part_]] so for example...

res[["x"]][["part1"]]
res[["y"]][["part2"]]
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top