[R] Function to crop p-values from multiple Anovas

micfalb-r at yahoo.de micfalb-r at yahoo.de
Wed Mar 23 18:17:04 CET 2011


Starting with data from a microarray experiment and I would like to analyse the influence of two factors (age, treatment) on gene expression.

Looking through the r-help archives and the web I tried the following:

I put my data in a dataframe similar to this one:
> example.df <- as.data.frame(matrix(data=runif(32,100,1000), nrow=4, ncol=4))
> example.df <- cbind(c("young","young","young","young","old","old","old","old"),c("drug", "control", "drug","control","drug","control","drug","control"), example.df)
> colnames(example.df) <- c(c("age","treatment"),paste("gene",1:4,sep=""))
> rownames(example.df) <- paste("sample", 1:8, sep="")
> example.df
age	treatment	 gene1	gene2	gene3	gene4
sample1	young	 drug		392	878	908	740
sample2	young	 control		167	263	711	392
sample3	young	 drug		155	252	242	547
sample4	young	 control		333	348	295	300
sample5	old	drug		392	878	908	740
sample6	old	control		167	263	711	392
sample7	old	drug		155	252	242	547
sample8	old	control		333	348	295	300

Now I would like to define a function that will crop the p-values from an Anova (so that I can use the function with a 'for loop' later on to go through all the genes):
> p.fun <- function(arg)	{
two_way_anova <- aov(arg  ~ age * treatment, data = example.df)
two_way_sum <- summary(two_way_anova)
p_values <- two_way_sum[[1]]$"Pr(>F)"[1:3]
return(p_values)
}

Unfortunately my setup seems to be flawed as I'm not capable to call my function:
> p.fun(gene1)
Error in eval(expr, envir, enclos) : object 'gene1' not found
> p.fun("gene1")
Error in model.frame.default(formula = arg ~ age * treatment, data = example.df,  : 
  variable lengths differ (found for 'age')

Any help on this would be truly appreciated.

Many thanks,
Mic Falb
University of Heidelberg






More information about the R-help mailing list