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.dalga...@biostat.ku.dk) FAX: (+45) 35327907
______________________________________________
R-help@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.