[R] Cut Dates into bins
Jim Lemon
drjimlemon at gmail.com
Wed Jun 8 09:47:50 CEST 2016
Hi Tjun Kiat,
The following examples work for me. One uses the dates you have
specified, adding one weekly date to cover the range of your daily
dates, otherwise you will generate NAs. The second defines weekly
breaks for a year and then simulates daily dates throughout that year.
Remember that your weekly dates must span the range of daily dates.
SMA<-list()
# weekly dates with a date added to cover the range of daily dates
SMA$TIME_DATE<-c("1/6/2014","28/9/2014","17/6/2014","19/9/2014",
"17/1/2015","13/1/2015","21/10/2014")
weekly_date<-as.Date(SMA$TIME_DATE,"%d/%m/%Y")
Shipment<-list()
# these are the daily dates
Shipment$DateRequire<-c("2014-06-09","2014-06-16","2014-06-16",
"2014-06-16","2014-06-17","2014-06-23")
daily_date<-as.Date(Shipment$DateRequire,"%Y-%m-%d")
table(cut(daily_date,breaks=weekly_date,include.lowest=TRUE,
labels=paste("Week",1:6,sep=" ")))
# here's an example with more dates over a full year
daily_date<-as.Date(paste("2000",1:12,sample(1:28,52,TRUE),sep="-"),
"%Y-%m-%d")
weekly_date<-seq(as.Date("01/01/2000","%d/%m/%Y"),
as.Date("31/12/2000","%d/%m/%Y"),by=7)
# no table here as it would be fairly long
cut(daily_date,breaks=weekly_date,include.lowest=TRUE,
labels=paste("Week",1:52,sep=" "))
Jim
On Wed, Jun 8, 2016 at 12:44 PM, TJUN KIAT TEO <teotjunk at hotmail.com> wrote:
>
> It does not seem to work for me. I will show you exactly my data format
>
> SMA$TIME_DATE
>
> "28/9/2014" "17/6/2014" "19/9/2014" "17/1/2015" "13/1/2015"
> "21/10/2014"
>
>
> Shipment$DateRequire
>
> "2014-06-09" "2014-06-16" "2014-06-16" "2014-06-16" "2014-06-17"
> "2014-06-23"
>
> What I would like to do is the cut the first set of dates into the second
> set of dates
>
>
>
>
>
>
>
>> From: drjimlemon at gmail.com
>> Date: Fri, 3 Jun 2016 18:59:11 +1000
>> Subject: Re: [R] Cut Dates into bins
>> To: teotjunk at hotmail.com
>> CC: r-help at r-project.org
>
>>
>> Hi Tjun Kiat,
>> This seems to work:
>>
>> daily_date<-as.Date(paste("2000-01",1:28,sep="-"),"%Y-%m-%d")
>> weekly_date<-as.Date(paste(c(1,8,15,22,28),"01/2000",sep="/"),
>> "%d/%m/%Y")
>> cut(daily_date,breaks=weekly_date,include.lowest=TRUE,
>> labels=paste("Week",1:4))
>>
>> Jim
>>
>>
>> On Fri, Jun 3, 2016 at 6:00 PM, TJUN KIAT TEO <teotjunk at hotmail.com>
>> wrote:
>> > I have two set of dates
>> >
>> > 2000-01-01
>> >
>> > 01/01/2000
>> >
>> > The second one occurs weekly and the first one occurs in daily. I would
>> > like to bin the first set of dates into the second set of dates. What is the
>> > best way to do it?
>> >
>> > I tried converting both formats into numeric formats
>> >
>> >
>> > DateBase=sort(as.numeric(as.POSIXlt(unique(Shipment$DateRequire),format="%Y-%m-%D",origin="1900-01-01")))
>> >
>> >
>> > Compare=as.numeric(as.POSIXlt(SMA$TIME_DATE,format="%d/%m/%y",origin="01/01/1900"))
>> >
>> > But the numeric numbers turned out to be very different.
>> >
>> > Tjun Kiat
>> >
>> >
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> > 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.
More information about the R-help
mailing list