Calendar (again) manipulations in R -


i have code this:

today<-as.date(sys.date()) spec<-as.date(today-c(1:1000)) df<-data.frame(spec) stage.dates<-as.date(c('2015-05-31','2015-06-07','2015-07-01','2015-08-23','2015-09-15','2015-10-15','2015-11-03')) stage.vals<-c(1:8) stagedf<-data.frame(stage.dates,stage.vals) df['ismonthinstage']<-ifelse(format(df$spec,'%m')==(format(stagedf$stage.dates,'%m')),stagedf$stage.vals,0) 

this producing incorrect output, i.e.

df.spec, df.ismonthinstage 2013-05-01, 0 2013-05-02, 1 2013-05-03, 0 .... 2013-05-10, 1 

it seems looping around, stage.dates 8 long, , repeating 'true' match every 8th. how fix flag 1 whole month in stage vals?

or bonus reputation - how set between different stage.dates, populate 1, 2, 3, etc of recent stage?

for example:

31st of may 7th of june populated 1, 7th of june 1st of july populated 2, etc, 3rd of november 30th of may populated 8?

thanks

edit:

i appreciate latter functionally different former question. trying arrive @ both (for different reasons), answers appreciated

see if works.

cut , split data based on stage.dates consider them buckets. don't need btw stage.vals here.

cut , split

data<-split(df, cut(df$spec, stagedf$stage.dates, include.lowest=true)) 

this should give list of data.frame splitted per stage.dates

now mutate data index..this stage.vals going be

mutate

data<-lapply(seq_along(data), function(index) {mutate(data[[index]], ismonthinstage=index)}) 

now join data frame in list using ldply

join

data=ldply(data) 

this give out or order dates can arrange by

sort

arrange(data,spec) 

final output

data[1:10,]          spec ismonthinstage 1  2015-05-31              1 2  2015-06-01              1 3  2015-06-02              1 4  2015-06-03              1 5  2015-06-04              1 6  2015-06-05              1 7  2015-06-06              1 8  2015-06-07              2 9  2015-06-08              2 10 2015-06-09              2 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -