[datatable-help] Sequential numbering for each month on a period of time

Bacou, Melanie mel at mbacou.com
Thu Dec 8 02:46:37 CET 2016


Sudhir,
Read the documentation for R function `as.Date()`, e.g. you can 
transform your factor column to a date column using `as.Date(my_date, 
format = "%d%b'%Y")`, then order/sort on that column.

I guess what you're looking for is:

dt[, my_date := as.Date(paste(1, my_date), format = "%d %b'%Y")]
month_seq <- seq("2006-04-01", "2014-08-01", "month")
dt_month <- data.table(month_int=1:length(month_seq), month_seq=month_seq)
setkey(dt, my_date)
setkey(dt_month, month_seq)
dt[dt_month, my_date_int := month_int]

--Mel.


On 12/7/2016 7:14 PM, jim holtman wrote:
> Please at least supply a sample of the data.  Is there more than one 
> row per month, are there months missing, if so how to handle.  You 
> left a lot unspecified.  Guidance can be provided with data.
>
>
> Jim Holtman
> Data Munger Guru
>
> What is the problem that you are trying to solve?
> Tell me what you want to do, not how you want to do it.
>
> On Tue, Dec 6, 2016 at 1:34 PM, kbsudhir <kbsudhir at gmail.com 
> <mailto:kbsudhir at gmail.com>> wrote:
>
>     Hi All,
>
>     I am a beginner in R.
>
>     I have a period of dates in my data set starting 10th April'2006
>     till 31st
>     Aug'2016. Its a period of 125 months. I want to sequentially
>     identify each
>     month with a number starting "1" in a new corresponding column Ex
>     "Month_Identifier". Ex
>     Apr'2006 - 1
>     May'2006 - 2 ........ Aug'2016 - 125
>
>     Also, the datatype of the column where date is available is a factor.
>
>     Requesting guidance on how to achieve this.
>
>     Regards
>     Sudhir
>
>
>
>     --
>     View this message in context:
>     http://r.789695.n4.nabble.com/Sequential-numbering-for-each-month-on-a-period-of-time-tp4727126.html
>     <http://r.789695.n4.nabble.com/Sequential-numbering-for-each-month-on-a-period-of-time-tp4727126.html>
>     Sent from the datatable-help mailing list archive at Nabble.com.
>     _______________________________________________
>     datatable-help mailing list
>     datatable-help at lists.r-forge.r-project.org
>     <mailto:datatable-help at lists.r-forge.r-project.org>
>     https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help
>     <https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help>
>
>
>
>
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/datatable-help/attachments/20161207/f3ca2e8c/attachment.html>


More information about the datatable-help mailing list