Re: [R] summing values by week - based on daily dates - but with somedates missing

From: Martyn Byng <Martyn.Byng_at_nag.co.uk>
Date: Wed, 30 Mar 2011 17:53:39 +0100

Hi,

How about something like:

sum.by.day <- function(ff) {
  by.day <- split(ff$value,weekdays(ff$dates))   lapply(by.day,sum)
}

by.grp <- split(myframe,myframe$group)

lapply(by.grp,sum.by.day)

Martyn

-----Original Message-----
From: r-help-bounces_at_r-project.org [mailto:r-help-bounces_at_r-project.org] On Behalf Of Dimitri Liakhovitski
Sent: 30 March 2011 15:23
To: r-help
Subject: [R] summing values by week - based on daily dates - but with somedates missing

Dear everybody,

I have the following challenge. I have a data set with 2 subgroups, dates (days), and corresponding values (see example code below). Within each subgroup: I need to aggregate (sum) the values by week - for weeks that start on a Monday (for example, 2008-12-29 was a Monday).
I find it difficult because I have missing dates in my data - so that sometimes I don't even have the date for some Mondays. So, I can't write a proper loop.
I want my output to look something like this: group dates value

group.1 2008-12-29  3.0937
group.1 2009-01-05  3.8833
group.1 2009-01-12  1.362

...
group.2 2008-12-29  2.250
group.2 2009-01-05  1.4057
group.2 2009-01-12  3.4411

...

Thanks a lot for your suggestions! The code is below: Dimitri

### Creating example data set:
mydates<-rep(seq(as.Date("2008-12-29"), length = 43, by = "day"),2) myfactor<-c(rep("group.1",43),rep("group.2",43)) set.seed(123)
myvalues<-runif(86,0,1)
myframe<-data.frame(dates=mydates,group=myfactor,value=myvalues)
(myframe)

dim(myframe)

## Removing same rows (dates) unsystematically: set.seed(123)
removed.group1<-sample(1:43,size=11,replace=F) set.seed(456)
removed.group2<-sample(44:86,size=11,replace=F) to.remove<-c(removed.group1,removed.group2);length(to.remove) to.remove<-to.remove[order(to.remove)]
myframe<-myframe[-to.remove,]
(myframe)

-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com

______________________________________________
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.

________________________________________________________________________
This e-mail has been scanned for all viruses by Star.\ _...{{dropped:12}}

______________________________________________
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 30 Mar 2011 - 17:00:56 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 30 Mar 2011 - 17:20:26 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