[R] Producing a table with mean values

arun smartpink111 at yahoo.com
Sat Sep 8 02:14:24 CEST 2012


Hi,
Try this:
dat1<-read.table(text="
 Seamount    Pico    Nano  Micro    Total_Ch
  1 0.0691 0.24200 0.00100  0.31210
  1 0.0938 0.00521 0.02060  0.11961
  1 0.1130 0.20000 0.06620  0.37920
  1 0.0864 0.15900 0.22300  0.46840
  1 0.0262 0.04570 0.00261  0.07451
  2 0.0314 0.17400 0.12800  0.33340
",sep="",header=TRUE,stringsAsFactors=FALSE)

library(data.table)
dat2<-data.table(dat1)
dat2mean<-dat2[,list(Pico=.Internal(mean(Pico)),Nano=.Internal(mean(Nano)),Micro=.Internal(mean(Micro))),by="Seamount"]
dat2mean
#   Seamount   Pico     Nano    Micro
#1:        1 0.0777 0.130382 0.062682
#2:        2 0.0314 0.174000 0.128000
dat2sd<-dat2[,list(sdPico=sd(Pico),sdNano=sd(Nano),sdMicro=sd(Micro)),by="Seamount"] 
dat2sd
#  Seamount     sdPico   sdNano    sdMicro
#1:        1 0.03281463 0.101197 0.09340563
#2:        2         NA       NA         NA

 dat3comb<-data.frame(cbind(dat2mean,dat2sd))
 dat3comb[,-5]
  Seamount   Pico     Nano    Micro     sdPico   sdNano    sdMicro
1        1 0.0777 0.130382 0.062682 0.03281463 0.101197 0.09340563
2        2 0.0314 0.174000 0.128000         NA       NA         NA


A.K.



----- Original Message -----
From: Tinus Sonnekus <tsonnekus at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Friday, September 7, 2012 4:49 PM
Subject: [R] Producing a table with mean values

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

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.





More information about the R-help mailing list