mean of individual rows for subsets of columns in an ExprSet
1
0
Entering edit mode
Claire Wilson ▴ 280
@claire-wilson-273
Last seen 10.2 years ago
HI Anna, I had a similar problem myself and didn't come up with an easy way to solve it, so my approach was res <- matrix(nrow=length(rownames(x)),ncol=2, byrow=T) for (i in 1:length(rownames(x))){ res[i,1] <- ((x[i,4]+x[i,5]+x[i,6])+x[i,7])/4) res[i,2] <- #calculate the SD here } you could probably set this up as a function allowing you to select different columns each time or if can assign your different columns to groups (maybe assign those you want mean and SD for as 1 and those you don't as 0) you could do something like groups <- c(0,0,0,1,1,1,1) calc.means <-function(x, y){ by(x, y, mean) } apply(eset@exprs,1 calc.means, y=groups) if you have a phenoData variable that distinguishes those columns you want from those you don't then even better, as you can use esApply, this can split your data according to a particular phenoData variable calc.means <-function(x){ by(x, <phenodata variable="">, mean) } esApply(eset@exprs,1 calc.means) hope this helps claire > -----Original Message----- > From: Anna Gustafsson [mailto:annag@biotech.kth.se] > Sent: 20 August 2003 15:39 > To: bioconductor@stat.math.ethz.ch > Subject: [BioC] mean of individual rows for subsets of columns in an > ExprSet > > > Dear all, > > I am sorry to disturb with yet a simple question but I have > tried my best and have now given up on solving it myself... :( > > I wonder if somebody knows how I easily can create mean > values and their SD from subsets of columns (representing > cases) over all rows individually (representing the genes) > from an object - eset - (which is an Expression Set with > 12625 genes, 7 samples). > E.g the mean and SD from all rows individually in columns > 4,5,6 and 7 of my data? > > Grateful for help! > > // Anna :o) > > _______________________________________________ > Bioconductor mailing list > Bioconductor@stat.math.ethz.ch > https://www.stat.math.ethz.ch/mailman/listinfo/bioconductor > -------------------------------------------------------- This email is confidential and intended solely for the use o...{{dropped}}
ASSIGN ASSIGN • 890 views
ADD COMMENT
0
Entering edit mode
@vincent-j-carey-jr-4
Last seen 9 weeks ago
United States
> HI Anna, > > I had a similar problem myself and didn't come up with an easy way to solve it, so my approach was > > res <- matrix(nrow=length(rownames(x)),ncol=2, byrow=T) > for (i in 1:length(rownames(x))){ > res[i,1] <- ((x[i,4]+x[i,5]+x[i,6])+x[i,7])/4) > res[i,2] <- #calculate the SD here > } i could not tell from the e-mail whether column statistics or row statistics were intended. for row statistics, commands of the form apply(x,1,f) can be used. if f returns a scalar on vector input (as does the function mean) then apply(x,1,f) is the vector with ith element f(x[i,]). you could then use apply(x[,4:7],1,mean) to do the first calculation above (and could easily modify to median or trimmed mean with this approach). if you want to be a little more elegant, you can write a function that returns the vector of statistics of interest msd <- function(x) c(mean(x),sqrt(var(x))) now apply(x,1,msd) returns a 2xn matrix where n is the number of rows of x. msdmat <- t(apply(x,1,msd)) is like your "res" above. lessons: use apply and R functions whenever feasible. > > > you could probably set this up as a function allowing you to select different columns each time or if can assign your different columns to groups (maybe assign those you want mean and SD for as 1 and those you don't as 0) you could do something like > > groups <- c(0,0,0,1,1,1,1) > calc.means <-function(x, y){ > by(x, y, mean) > } > apply(eset@exprs,1 calc.means, y=groups) this can be done in one step using subscripting within the apply msdmat <- t(apply(x[,groups==1],1,msd)) # or specify the groups explicitly in the subscripting NB: please don't use the "@" notation if it can be avoided. we provide "accessor" function exprs() that should be used.
ADD COMMENT

Login before adding your answer.

Traffic: 519 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6