[R] calculate monthly mean

Joshua Wiley jwiley.psych at gmail.com
Fri Sep 3 08:36:22 CEST 2010


Hello Ali,

It looks to me like you have two questions: 1) the error you get from
scatterplot3d 2) how to calculate the monthly mean.

If you could provide some sample data that would make it a lot easier
to help you understand why the error occurred and what to do about it.
 For now, maybe this will get you started on calculating the monthly
mean.

# Create a data frame with the data in 'x' and the dates in 'y'
sampdat <- data.frame(x = 1:365, y = Sys.Date() + 1:365)
# convert 'y' to a factor by cut()ing the dates by month
sampdat$y <- cut.Date(x = sampdat$y, breaks = "month",
                      include.lowest = TRUE, right = TRUE)
# now just find the mean of 'x' for each level of 'y'
by(data = sampdat$x, INDICES = sampdat$y, FUN = mean)


# For some documentation
?Date
?cut.Date #cut() will call the right method, but I wanted to be explicit
?by

Cheers,

Josh

On Thu, Sep 2, 2010 at 5:06 PM, Ali Alsamawi <ali.alsamawi at unsw.edu.au> wrote:
>
>
> Hello group
>
>
>       Im trying to plot 3d  with scatterplot packages, i got error say  " length(color) must be equal length(x) or 1 " may data has dimensions (lon,lat,lev,time) ,the time in month i want to
> calculate the monthly mean for the time how can i make that , is there any function doing that
>
> Thanks a lot
>
>
>
>
>
>
>
>
>
> ##load rgl package
> library(rgl)
> library(fields)
> library(ncdf)
> library(scatterplot3d)
> ## open binary file to read
>     nc <- open.ncdf("/srv/ccrc/data05/z3236814/mm5/co2/2000/q.200001.mon.nc")
>
> v1 <- nc$var [[1]]
>
> v2 <- nc$var [[2]]
>
> v3 <- nc$var [[3]]
>
> data1 <- get.var.ncdf(nc,v1)
> data2 <- get.var.ncdf(nc,v2)
> data3 <- get.var.ncdf(nc,v3)
>
>
> coldat = data1[1:111,1:101,23,1:60]
>
> ## creat colour
> hcol = cumsum(coldat)
> coldat = hcol
> hcol = hcol/max(hcol,na.rm=TRUE)
>
>
> col <- hsv(h=hcol,s=1,v=1)
>
> X <-scatterplot3d(data3[1:111,1:101],data2[1:111,1:101],data1[1:111,1:101,23,1:60],color=col)
> ______________________________________________
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list