[R] colmeans not working
arun
smartpink111 at yahoo.com
Mon Dec 24 04:10:51 CET 2012
HI Eliza,
The reason why you didn't get the result is because:
dat1<-read.csv("elisa.csv",header=TRUE,stringsAsFactors=FALSE)
str(dat1)
#'data.frame': 506953 obs. of 5 variables:
# $ st : chr "AGOMO" "AGOMO" "AGOMO" "AGOMO" ...
# $ year : int 2004 2004 2004 2004 2004 2004 2004 2004 2004 2004 ...
# $ month : int 1 1 1 1 1 1 1 1 1 1 ...
# $ day : int 1 2 3 4 5 6 7 8 9 10 ...
# $ discharge: num 6.75 7.6 5.58 5.02 4.8 ...
res<-lapply(split(dat1,dat1$st),function(x) dcast(x,month~year,mean,value.var="discharge"))
res1<-lapply(res,function(x) dim(x[,-1])) #upon checking the results here "$UZZCO NULL"
res["UZZCO"]
#$UZZCO
# month 2010
#1 1 0.3157691
#2 2 1.3984538
#3 3 3.0489628
#4 4 1.1438021
#5 5 1.5077654
#6 6 0.2834110
#7 7 0.1296051
#8 8 0.2399172
#9 9 0.1547817
#10 10 0.1488598
#11 11 1.8002791
#12 12 1.8530978
res2<-lapply(res,function(x) {if(is.data.frame(x[,-1])) colMeans(x[,-1]) else mean(x[,-1])}) #colMeans
res3<-lapply(res,function(x) {if(is.data.frame(x[,-1])) rowMeans(x[,-1]) else mean(x[,-1])}) #rowMeans
head(res2,3)
#$AGOMO
# 2004 2005 2006 2007 2008 2009 2010
#3.122868 1.772765 2.109678 1.189568 5.351325 6.058146 6.483248
#
#$AGONO
# 2002 2003 2004 2005 2006 2007 2008 2009
#14.877080 5.840740 9.837951 4.820480 7.701798 6.270975 9.110857 10.942145
# 2010
#12.762130
#
#$ANZMA
# 2003 2004 2005 2006 2007 2008 2009
#2.669676 3.358456 3.057410 2.472686 2.810164 4.595553 5.139108
head(res3,2)
#$AGOMO
# 1 2 3 4 5 6 7 8
#2.632842 3.720805 2.698315 6.368157 7.226502 3.336990 1.223611 1.592963
# 9 10 11 12
#2.206013 1.657038 5.381786 6.676574
#
#$AGONO
# 1 2 3 4 5 6 7 8
#8.629570 10.066012 8.624739 8.591345 13.773817 5.386458 2.847495 5.448138
# 9 10 11 12
#9.266519 7.333363 15.249745 14.335008
Hope it helps.
A.K.
________________________________
From: eliza botto <eliza_botto at hotmail.com>
To: "smartpink111 at yahoo.com" <smartpink111 at yahoo.com>
Cc: "r-help at r-project.org" <r-help at r-project.org>
Sent: Sunday, December 23, 2012 9:10 PM
Subject: RE: [R] colmeans not working
Dear Arun,
on having a bird eye view i came to notice that the data you are using dont have month column at the beginning.
my initial data was
> res
$EE
month 2005 2006 2008 2009
1 1 1.7360776 0.8095275 1.6369044 0.8195241
2 2 0.6962079 3.8510720 0.4319758 2.3304495
3 3 1.0423625 2.7687266 0.2904245 0.7015527
4 4 2.4158326 1.2315324 1.4287387 1.5701019
$WW
month 2008 2009 2010
1 1 1.4737028 2.314878 2.672661
2 2 1.6700918 2.609722 2.112421
3 3 3.2387775 7.305766 6.939536
4 4 6.7063592 18.745256 13.278218
afterwards i used eliminated the month column and got
$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 when i m aPPLYING rowMeans or colMeans, i am getting error.
i m attaching the text file for better understandging
eliza
> Date: Sun, 23 Dec 2012 17:40:15 -0800
> From: smartpink111 at yahoo.com
> Subject: Re: [R] colmeans not working
> To: eliza_botto at hotmail.com
> CC: r-help at r-project.org; bbolker at gmail.com
>
> Hi Eliza,
>
> I tried with the example you gave. Couldn't reproduce the error.
>
>
> res1<-list(read.table(text="
> 2005 2006 2008 2009
> 1.7360776 0.8095275 1.6369044 0.8195241
> 0.6962079 3.8510720 0.4319758 2.3304495
> 1.0423625 2.7687266 0.2904245 0.7015527
> 2.4158326 1.2315324 1.4287387 1.5701019
> ",sep="",header=TRUE),read.table(text="
> 2008 2009 2010
> 1.4737028 2.314878 2.672661
> 1.6700918 2.609722 2.112421
> 3.2387775 7.305766 6.939536
> 6.7063592 18.745256 13.278218
> ",sep="",header=TRUE))
> names(res1)<-c("EE","WW")
> res1<-lapply(res1,function(x) {names(x)<-gsub("X","",names(x));x})
> res1
> #$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.473703 2.314878 2.672661
> #2 1.670092 2.609722 2.112421
> #3 3.238777 7.305766 6.939536
> #4 6.706359 18.745256 13.278218
> lapply(res1,colMeans)
> #$EE
> # 2005 2006 2008 2009
> #1.4726202 2.1652146 0.9470108 1.3554070
> #
> #$WW
> # 2008 2009 2010
> #3.272233 7.743906 6.250709
>
> lapply(res1,rowMeans)
> #$EE
> #[1] 1.250508 1.827426 1.200767 1.661551
> #
> #$WW
> #[1] 2.153747 2.130745 5.828026 12.909944
>
>
> A.K.
>
>
>
> ----- Original Message -----
> From: eliza botto <eliza_botto at hotmail.com>
> To: bbolker at gmail.com; r-help at stat.math.ethz.ch
> Cc:
> Sent: Sunday, December 23, 2012 7:48 PM
> Subject: Re: [R] colmeans not working
>
>
> Dear Ben,Thanks for replying but its still not working.your code was>lapply(res,colMeans)but i want to use "res1" instead of "res". when i did use it, i got same error.eliza
> > To: r-help at stat.math.ethz.ch
> > From: bbolker at gmail.com
> > Date: Mon, 24 Dec 2012 00:31:41 +0000
> > Subject: Re: [R] colmeans not working
> >
> > 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)
> >
> > ______________________________________________
> > 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.
>
> [[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