[R] Producing a table with mean values

Duncan Murdoch murdoch.duncan at gmail.com
Sat Sep 8 01:55:16 CEST 2012


On 12-09-07 4: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

Something like this could work:

library(tables)
tabular( Seamount ~ (Pico + Nano + Micro)*mean, data=SChla)

There are lots of variations, e.g. if you want the size classes as rows 
and the Seamounts as columns,

tabular( (Pico + Nano + Micro) ~ Seamount*mean, data=SChla)

It will also output LaTeX tables if you ask for them.

Duncan Murdoch

>
> 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.




More information about the R-help mailing list