Re: [R] cbind and mean by week

From: jim holtman <jholtman_at_gmail.com>
Date: Wed, 26 Mar 2008 06:02:08 -0500

Is this what you want? You have to add "na.rm=TRUE" to the calculations:

> week<-c(28,28,28,28,28,28,28,29,29,29,29,29,29,29,30,30,30,30,30,30,30)
> td<-c(0.015,0.012,NA,0.015,NA,0.014,0.014,0.013,0.013,0.013,0.013,NA,0.010,0.013,0.015,0.011,0.014,0.014,0.014,0.014,0.016)
> pd<-c(0,0,NA,80,NA,45,223,228,224,488,525,NA,80,417,413,373,914,213,651,521,979)
> x<-data.frame(week,td,pd)
> x

   week td pd
1 28 0.015 0
2 28 0.012 0
3 28 NA NA
4 28 0.015 80
5 28 NA NA

6    28 0.014  45
7    28 0.014 223
8    29 0.013 228
9    29 0.013 224
10   29 0.013 488
11   29 0.013 525

12 29 NA NA
13   29 0.010  80
14   29 0.013 417
15   30 0.015 413
16   30 0.011 373
17   30 0.014 914
18   30 0.014 213
19   30 0.014 651
20   30 0.014 521
21   30 0.016 979

> pmean<- tapply(x[,3], x$week, mean, na.rm=TRUE)
> pmean

      28 29 30
 69.6000 327.0000 580.5714
> pdmean<- data.frame(pmean)
> pdmean

      pmean

28  69.6000
29 327.0000
30 580.5714

> xweek <- aggregate(x, list(x$week), mean, na.rm=TRUE)
> xweek
  Group.1 week     td       pd
1      28   28 0.0140  69.6000
2      29   29 0.0125 327.0000
3      30   30 0.0140 580.5714

>

On Tue, Mar 25, 2008 at 10:18 PM, Felipe Carrillo <mazatlanmexico_at_yahoo.com> wrote:
> Hi:
> I have been able to finally crunch my data by
> importing it by week(thank you all for your help),but
> here we go again..
> Now I'am trying to do it for the the whole year.
> Since the
> dataset is huge I'm only making a 3 weeks dataframe.
> - I want to get the mean of pd by week
> - I want to count the number of days by week and bind
> it to the existing dataframe(x)
> - I want to remove the NA's by week and bind another
> column counting the rows after the NA's are removed.
> Don't think that I'am crazy but I need those fields
> for my annual report. I was trying to use some of
> the functions below but nothing seems to work..One
> of these days I'll be an R pro(we'll see about that)
> Thanks
>
> week<-c(28,28,28,28,28,28,28,29,29,29,29,29,29,29,30,30,30,30,30,30,30)
> td<-c(0.015,0.012,NA,0.015,NA,0.014,0.014,0.013,0.013,0.013,0.013,NA,0.010,0.013,0.015,0.011,0.014,0.014,0.014,0.014,0.016)
> pd<-c(0,0,NA,80,NA,45,223,228,224,488,525,NA,80,417,413,373,914,213,651,521,979)
> x<-data.frame(week,td,pd)
> x
> pmean<- tapply(x[,3], x$week, mean)
> pdmean
> pdmean<- data.frame(pmean)
> pdmean
> xweek <- aggregate(x, list(x$week), mean)
> xweek
>
> Felipe D. Carrillo
> Fishery Biologist
> Department of the Interior
> US Fish & Wildlife Service
> California, USA
>
>
>
> ____________________________________________________________________________________
> Be a better friend, newshound, and
>
> ______________________________________________
> 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.
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
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.
Received on Wed 26 Mar 2008 - 11:21:30 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 26 Mar 2008 - 12:30:24 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive