[R] colmeans not working

Ben Bolker bbolker at gmail.com
Mon Dec 24 01:31:41 CET 2012


eliza botto <eliza_botto <at> hotmail.com> writes:

>  Dear useRs,You must all the planning for the christmas, but i am
> stucked in my office on the following issue i had a file containg
> information about station name, year, month, day, and discharge
> information. i opened it by using following command

> > dat1<-read.table("EL.csv",header=TRUE, sep=",",na.strings="NA")

You can probably use 

dat1 <- read.csv("EL.csv") 

  (although you may have to double-check some of the other
default differences between read.csv and read.table, e.g.
quote and comment.char arguments)

> then by using following codes suggested by arun and rui i managed to obtain an
output

library(reshape2)
res <- lapply(split(dat1,dat1$st),
   function(x) dcast(x,month~year,mean,value.var="discharge"))

[snip]
 
res1 <- lapply(res, function(x)x[,-1])

  (c() is redundant here)

> $EE
>         2005      2006      2008      2009
> 1  1.7360776 0.8095275 1.6369044 0.8195241
> 2  0.6962079 3.8510720 0.4319758 2.3304495
> 3  1.0423625 2.7687266 0.2904245 0.7015527
> 4  2.4158326 1.2315324 1.4287387 1.5701019
> 
> $WW
>          2008      2009      2010
> 1   1.4737028  2.314878  2.672661
> 2   1.6700918  2.609722  2.112421
> 3   3.2387775  7.305766  6.939536
> 4   6.7063592 18.745256 13.278218
> 
> 

Now you just need

lapply(res,colMeans)




More information about the R-help mailing list