Hi R-helpers, I have been struggling with calculating row and column statistics, e.g. standard deviation. I know that> datac$Mean<-rowMeans(datac,na.rm=TRUE)will give me row means. I have tried to replicate those row means with the apply function:> datac$Mean2<-apply(datac,2,mean)so that I can replace the function argument with "sd" (instead of mean) to get standard deviations. But, I'm running into this error:> dim(datac)[1] 17 271> datac$Mean2<-apply(datac,2,mean)Error in dimnames(x) <- dn : length of 'dimnames' [2] not equal to array extent Can anyone see what I'm doing wrong? Thanks! Mark Na
Tena koe Mark I think you might want apply(datac, 1, mean) i.e., apply the function to the first dimension (rows) rather than the second (columns). HTH ... Peter Alspach> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of Mark Na > Sent: Tuesday, 23 June 2009 10:20 a.m. > To: r-help at r-project.org > Subject: [R] Calculating "row standard deviations" > > Hi R-helpers, > > I have been struggling with calculating row and column > statistics, e.g. standard deviation. > > I know that > > datac$Mean<-rowMeans(datac,na.rm=TRUE) > will give me row means. > > I have tried to replicate those row means with the apply function: > > datac$Mean2<-apply(datac,2,mean) > > so that I can replace the function argument with "sd" (instead of > mean) to get standard deviations. > > But, I'm running into this error: > > > dim(datac) > [1] 17 271 > > datac$Mean2<-apply(datac,2,mean) > Error in dimnames(x) <- dn : > length of 'dimnames' [2] not equal to array extent > > > Can anyone see what I'm doing wrong? > > Thanks! > > Mark Na > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >The contents of this e-mail are confidential and may be subject to legal privilege. If you are not the intended recipient you must not use, disseminate, distribute or reproduce all or any part of this e-mail or attachments. If you have received this e-mail in error, please notify the sender and delete all material pertaining to this e-mail. Any opinion or views expressed in this e-mail are those of the individual sender and may not represent those of The New Zealand Institute for Plant and Food Research Limited.
Mark Na wrote:> Hi R-helpers, > > I have been struggling with calculating row and column statistics, > e.g. standard deviation. > > I know that >> datac$Mean<-rowMeans(datac,na.rm=TRUE) > will give me row means. > > I have tried to replicate those row means with the apply function: >> datac$Mean2<-apply(datac,2,mean) > > so that I can replace the function argument with "sd" (instead of > mean) to get standard deviations. > > But, I'm running into this error: > >> dim(datac) > [1] 17 271 >> datac$Mean2<-apply(datac,2,mean) > Error in dimnames(x) <- dn : > length of 'dimnames' [2] not equal to array extent > > > Can anyone see what I'm doing wrong?Not really, but you need apply(datac,1,mean) for row means: > apply(airquality,2,mean) Ozone Solar.R Wind Temp Month Day NA NA 9.957516 77.882353 6.993464 15.803922 which are obviously column means. However, I see a different error if I try to assign that back > airquality$m <- apply(airquality,2,mean) Error in `$<-.data.frame`(`*tmp*`, "m", value = c(NA, NA, 9.95751633986928, : replacement has 6 rows, data has 153 Can't get much closer without a REPRODUCIBLE example. -- O__ ---- Peter Dalgaard ?ster Farimagsgade 5, Entr.B c/ /'_ --- Dept. of Biostatistics PO Box 2099, 1014 Cph. K (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
On Jun 22, 2009, at 6:19 PM, Mark Na wrote:> Hi R-helpers, > > I have been struggling with calculating row and column statistics, > e.g. standard deviation. > > I know that >> datac$Mean<-rowMeans(datac,na.rm=TRUE) > will give me row means. > > I have tried to replicate those row means with the apply function: >> datac$Mean2<-apply(datac,2,mean) > > so that I can replace the function argument with "sd" (instead of > mean) to get standard deviations. > > But, I'm running into this error: > >> dim(datac) > [1] 17 271 >> datac$Mean2<-apply(datac,2,mean) > Error in dimnames(x) <- dn : > length of 'dimnames' [2] not equal to array extentIf you are trying to create a group means value for each element in an array or data.frame then the function to use is ave with its default function is mean. Other functions can be used but that is not necessary here. You could try: datac$Mean2<-apply(datac,2,ave)> > > Can anyone see what I'm doing wrong? > > Thanks! > > Mark NaDavid Winsemius, MD Heritage Laboratories West Hartford, CT