[R] Producing a table with mean values

David Winsemius dwinsemius at comcast.net
Sat Sep 8 01:30:21 CEST 2012


On Sep 7, 2012, at 1:49 PM, Tinus Sonnekus wrote:

> Hi All,
> 
> I have a data set wit three size classes (pico, nano and micro) and 12
> different sites (Seamounts). I want to produce a table with the mean and
> standard deviation values for each site.
> 
>     Seamount     Pico    Nano   Micro    Total_Ch
> 1 Off_Mount 1 0.0691 0.24200 0.00100  0.31210
> 2 Off_Mount 1 0.0938 0.00521 0.02060  0.11961
> 3 Off_Mount 1 0.1130 0.20000 0.06620  0.37920
> 4 Off_Mount 1 0.0864 0.15900 0.22300  0.46840
> 5 Off_Mount 1 0.0262 0.04570 0.00261  0.07451
> 6 Off_Mount 2 0.0314 0.17400 0.12800  0.33340

Assuming this is a dataframe named "dat"...

> with(dat, aggregate(cbind(Pico, Nano, Micro), list(Seamount), FUN=mean))
    Group.1       Pico      Nano      Micro
1 Off_Mount 0.06998333 0.1376517 0.07356833


-- 
David.

> 
> I tried the following script but get an error message
> 
> *Error in results[i, "u.Pico", "u.Nano", "u.Micro"] <- sapply(z, mean) : *
> *  incorrect number of subscripts *
> 
> The code I used:
> 
> *SChla <- read.csv("SM_Chla_data.csv")*
> *sm <- as.character(unique(SChla$Seamount))*
> *
> *
> *results <-
> matrix(NA,nrow=length(sm),ncol=6,dimnames=list(sm,c("u.Pico","u.Nano","u.Micro","sd.Pico","sd.Nano","sd.Micro")))
> *
> *
> *
> *for (i in sm){*
> *z <- subset(SChla, Seamount==i, select=c(Pico, Nano, Micro))*
> *results[i,"u.Pico","u.Nano","u.Micro"] <- sapply(z, mean)*
> *results[i,"sd.Pico","sd.Nano","sd.Micro"] <- sapply(z, sd)*
> *}*
> *
> *
> *print(results)*
> 
> Please can some one advise me how to fix the error or maybe have an
> alternative solution I will appreciate it.
> 
> Thank you.
> Tinus
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.

David Winsemius, MD
Alameda, CA, USA




More information about the R-help mailing list