[R] colmeans not working

arun smartpink111 at yahoo.com
Mon Dec 24 02:28:06 CET 2012


HI Eliza,
Just a doubt:
YOu mentioned that
 "i then eliminated the first column as i was interested in knowing the row wise mean of each sublist, by using" and 
"afterwards when i applied the "colMeans" command i was not able to calculate the mean as i got the following error"

Do you need colMeans or rowMeans?

From the subset of data from your earlier email, I was able to get both rowMeans or colMeans.

dat1<-read.table("Eliza.txt",sep="",header=TRUE,stringsAsFactors=FALSE)
library(reshape2)
 res<-lapply(split(dat1,dat1$st.),function(x) dcast(x,month~year,mean,value.var="population_in_million"))

lapply(res,function(x) colMeans(x[,-1]))
#$Sa
#1955 1956 1957 1958   #didn't remove the NaN's
# NaN  NaN  NaN  NaN 

#$Ta
 #   1966     1967     1968     1969 
#2.355556 2.400000 2.355556 2.400000 


 lapply(res,function(x) rowMeans(x[,-1]))
#$Sa
#  1   2   3   4   5   6   7   8   9  10  11  12 
#NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN 
#
#$Ta
  #     1        2        3 
#2.400000 2.400000 2.333333 
A.K.



----- Original Message -----
From: eliza botto <eliza_botto at hotmail.com>
To: "r-help at r-project.org" <r-help at r-project.org>
Cc: 
Sent: Sunday, December 23, 2012 7:17 PM
Subject: [R] colmeans not working


[text file is also attached in case you find the format of email difficult to understand]
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")
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"))
> res
$EE   month      2005      2006      2008      20091      1 1.7360776 0.8095275 1.6369044 0.81952412      2 0.6962079 3.8510720 0.4319758 2.33044953      3 1.0423625 2.7687266 0.2904245 0.70155274      4 2.4158326 1.2315324 1.4287387 1.5701019
$WW   month       2008      2009      20101      1  1.4737028  2.314878  2.6726612      2  1.6700918  2.609722  2.1124213      3  3.2387775  7.305766  6.9395364      4  6.7063592 18.745256 13.278218
i then eliminated the first column as i was interested in knowing the row wise mean of each sublist, by using

res1 <- lapply(res, function(x)x[,-c(1) ])
$EE        2005      2006      2008      20091  1.7360776 0.8095275 1.6369044 0.81952412  0.6962079 3.8510720 0.4319758 2.33044953  1.0423625 2.7687266 0.2904245 0.70155274  2.4158326 1.2315324 1.4287387 1.5701019
$WW         2008      2009      20101   1.4737028  2.314878  2.6726612   1.6700918  2.609722  2.1124213   3.2387775  7.305766  6.9395364   6.7063592 18.745256 13.278218

afterwards when i applied the "colMeans" command i was not able to calculate the mean as i got the following error
>'x' must be an array of at least two dimensions
i cant spot the mistake in the last 5 hours.
you help is neededthanks in advance
eliza                            
______________________________________________
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