hi, I have some session data in a dataframe, where each session is recorded with a start and a stop date. Like this: session_start session_stop ==================2009-01-03 2009-01-04 2009-01-01 2009-01-05 2009-01-02 2009-01-09 A session is at least one day long. Now I want a dataframe with 'active sessions' per date. Like this: date active_sessions ============2009-01-01 1 2009-01-02 2 2009-01-03 3 2009-01-04 3 2009-01-05 2 2009-01-06 1 2009-01-07 1 2009-01-08 1 2009-01-09 1 How do I do that? I've searched the usual sources, but my newbie status and language barrier left me with nothing. So plz, anyone?
Try this:> dateseq <- function(i) seq(DF[i, 1], DF[i, 2], 1) > table(as.Date(unlist(lapply(1:nrow(DF), dateseq)), origin = "1970-01-01"))2009-01-01 2009-01-02 2009-01-03 2009-01-04 2009-01-05 2009-01-06 2009-01-07 1 2 3 3 2 1 1 2009-01-08 2009-01-09 1 1 On Mon, Feb 9, 2009 at 10:57 AM, <stefan.petersson at inizio.se> wrote:> > hi, > > I have some session data in a dataframe, where each session is recorded with a start and a stop date. Like this: > > session_start session_stop > ==================> 2009-01-03 2009-01-04 > 2009-01-01 2009-01-05 > 2009-01-02 2009-01-09 > > A session is at least one day long. Now I want a dataframe with 'active sessions' per date. Like this: > > date active_sessions > ============> 2009-01-01 1 > 2009-01-02 2 > 2009-01-03 3 > 2009-01-04 3 > 2009-01-05 2 > 2009-01-06 1 > 2009-01-07 1 > 2009-01-08 1 > 2009-01-09 1 > > How do I do that? I've searched the usual sources, but my newbie status and language barrier left me with nothing. So plz, anyone? > > ______________________________________________ > 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. >
On Mon, Feb 9, 2009 at 4:57 PM, <stefan.petersson at inizio.se> wrote:> > hi, > > I have some session data in a dataframe, where each session is recorded with a start and a stop date. Like this: > > session_start session_stop > ==================> 2009-01-03 2009-01-04 > 2009-01-01 2009-01-05 > 2009-01-02 2009-01-09 > > A session is at least one day long. Now I want a dataframe with 'active sessions' per date. Like this: > > date active_sessions > ============> 2009-01-01 1 > 2009-01-02 2 > 2009-01-03 3 > 2009-01-04 3 > 2009-01-05 2 > 2009-01-06 1 > 2009-01-07 1 > 2009-01-08 1 > 2009-01-09 1 > > How do I do that? I've searched the usual sources, but my newbie status and language barrier left me with nothing. So plz, anyone? >Hej Stefan, The following should do. It's a bit convoluted though, so someone else might be able to come up with a better solution.> teststart stop 1 2009-01-03 2009-01-04 2 2009-01-01 2009-01-05 3 2009-01-02 2009-01-09 activeDaysPerSession<-apply(test,MARGIN=1,FUN=function(x) seq(from=as.Date(x["start"]), to=as.Date(x["stop"]),by=1 ) ) ActiveDays<-as.Date(unlist(activeDaysPerSession)) as.data.frame(table(ActiveDays)) Regards, Gustaf -- Gustaf Rydevik, M.Sci. tel: +46(0)703 051 451 address:Essingetorget 40,112 66 Stockholm, SE skype:gustaf_rydevik