Amy Mikhail
2009-Jan-07 15:23 UTC
[Rd] Function to recognise convert dates between gregorian and other calendars (e.g. Persian)?
Dear list, I will shortly have some data that contains numeric dates in the Persian / Jalali calendar format, which I would like to convert to gregorian. At the moment there doesn't seem to be a function for this in R, but it would be great if someone could come up with same - I would attempt it but the algorithm is very complex and this is also way beyond my fairly rudimentary knowledge of R. There is an informative article about how this can be done in C by Homam Hosseini here: http://www.codeproject.com/KB/cs/JalaliCalendar.aspx and a different attempt here: http://www.projectpluto.com/calendar.htm#jalali ... and several other examples (mostly in C I think) can be found by googling Persian Jalali calendar. Aside from being written in C, the other problem with all the code snippets I found is that they are only for converting single dates - whereas I would like to convert an entire column of dates within a data.frame. To do this I would need to be able to first set the date class of my date column as Persian/Jalali and then have a function to convert these dates to gregorian. I originally posted my question to the R-SIG-epi list (on the off-chance that this might be a common problem and someone else had already written code for it); Bendix Carstensen has suggested that someone with both C and R knowledge might be able to develop a function to do this for R, perhaps in consultation with Homam Hosseini if converting his code to R or using his approach would be a helpful starting point. Any takers? With best wishes, Amy -- Amy Mikhail Program Manager, ACTc Afghanistan Malaria Project London School of Hygiene & Tropical Medicine HPRO, Charahi-e-Shahid, Shar-e-Naw, Kabul, Afghanistan Email (Afg): amy.mikhail@googlemail.com Email (UK): Amy.Mikhail@lshtm.ac.uk Tel (Afg): +93 (0)706 126627 Tel (UK): +44 (0)781 4176107 [[alternative HTML version deleted]]
Ted Byers
2009-Jan-07 17:20 UTC
[Rd] Function to recognise convert dates between gregorian and other calendars (e.g. Persian)?
On Wed, Jan 7, 2009 at 10:23 AM, Amy Mikhail <amy.mikhail@googlemail.com>wrote:> Dear list, > > I will shortly have some data that contains numeric dates in the Persian / > Jalali calendar format, which I would like to convert to gregorian. At the > moment there doesn't seem to be a function for this in R, but it would be > great if someone could come up with same - I would attempt it but the > algorithm is very complex and this is also way beyond my fairly rudimentary > knowledge of R. > > How do you feel about mixed language programming?I don't know anything about Jalali dates, but I took the time to check and found that Perl has modules that handle this (use CPAN or, on windows, PPM to find them). However, like the C code you found, it will convert values a date at a time. I don't know why this would be an issue. I have never tried to use either C or perl from within R, but if you can handle that, it would be trivial to apply these function calls to each value in a vector (or array if you prefer) in functions written in either C/C++ or perl. In both C++ using STL and Perl, that would require only one line of code, and perhaps a couple more in C to manage the required loop if you restrict yourself to C, ignoring the benefits of C++. If I were doing this, I'd do it even before storing the data in my database, or at least before importing it into a dataframe in R, but that is primarily because I am still learning R, having used it for only a few months, rather than a few years using perl and 15+ years using C++. I am so early on my R learning curve that I haven't yet looked at writing code in C++ or Perl that is to be called by R. While I haven't read through it in enough detail to play seriously with it, the method for using such code from with R described in "Writing R Extensions<file:///C:/Program%20Files/R/R-2.8.0/doc/manual/R-exts.html>" seems simple enough. Instead of writing the code to implement your 'complex' algorithm, why not just use the code you've found, or that available in CPAN (I don't know about you, but I hate reinventing the wheel), and create the trivial extension needed following the instructions in "Writing R Extensions<file:///C:/Program%20Files/R/R-2.8.0/doc/manual/R-exts.html>", or do it to the raw data before you import it into R? HTH Ted [[alternative HTML version deleted]]
Amy Mikhail
2009-Jan-07 18:22 UTC
[Rd] Function to recognise convert dates between gregorian and other calendars (e.g. Persian)?
Hi Ted,> > Many thanks for the suggestion. I'm afraid I'm not from a programming > background at all, hence I wouldn't even know what software to paste the > c-code into, much less how to apply it to my column of dates. I have been > using R for a few years but very much as a user rather than developer - so > I'm comfortable with R syntax but syntax in other languages is pretty alien > to me. Actually the data will be entered into an MS Access database - I've > tried asking whether there is a suitable workaround in MS access forums as > well, but so far no joy - not sure if it is possible to somehow paste the C > code in there - if so would need some detailed instructions on how to do > this. > > I hadn't thought to check out the R extensions before, if this is > straightforward I'll give it a go... > > Thanks and best wishes, > Amy > > > On Wed, Jan 7, 2009 at 5:20 PM, Ted Byers <r.ted.byers@gmail.com> wrote: > >> On Wed, Jan 7, 2009 at 10:23 AM, Amy Mikhail <amy.mikhail@googlemail.com>wrote: >> >>> Dear list, >>> >>> I will shortly have some data that contains numeric dates in the Persian >>> / >>> Jalali calendar format, which I would like to convert to gregorian. At >>> the >>> moment there doesn't seem to be a function for this in R, but it would be >>> great if someone could come up with same - I would attempt it but the >>> algorithm is very complex and this is also way beyond my fairly >>> rudimentary >>> knowledge of R. >>> >>> How do you feel about mixed language programming? >> >> I don't know anything about Jalali dates, but I took the time to check and >> found that Perl has modules that handle this (use CPAN or, on windows, PPM >> to find them). However, like the C code you found, it will convert values a >> date at a time. I don't know why this would be an issue. >> >> I have never tried to use either C or perl from within R, but if you can >> handle that, it would be trivial to apply these function calls to each value >> in a vector (or array if you prefer) in functions written in either C/C++ or >> perl. In both C++ using STL and Perl, that would require only one line of >> code, and perhaps a couple more in C to manage the required loop if you >> restrict yourself to C, ignoring the benefits of C++. >> >> If I were doing this, I'd do it even before storing the data in my >> database, or at least before importing it into a dataframe in R, but that is >> primarily because I am still learning R, having used it for only a few >> months, rather than a few years using perl and 15+ years using C++. I am so >> early on my R learning curve that I haven't yet looked at writing code in >> C++ or Perl that is to be called by R. While I haven't read through it in >> enough detail to play seriously with it, the method for using such code from >> with R described in "Writing R Extensions" seems simple enough. Instead >> of writing the code to implement your 'complex' algorithm, why not just use >> the code you've found, or that available in CPAN (I don't know about you, >> but I hate reinventing the wheel), and create the trivial extension needed >> following the instructions in "Writing R Extensions", or do it to the raw >> data before you import it into R? >> >> HTH >> >> Ted >> > > > > -- > Amy Mikhail > Program Manager, ACTc Afghanistan Malaria Project > London School of Hygiene & Tropical Medicine > HPRO, Charahi-e-Shahid, Shar-e-Naw, Kabul, Afghanistan > > Email (Afg): amy.mikhail@googlemail.com > Email (UK): Amy.Mikhail@lshtm.ac.uk > Tel (Afg): +93 (0)706 126627 > Tel (UK): +44 (0)781 4176107 >-- Amy Mikhail Program Manager, ACTc Afghanistan Malaria Project London School of Hygiene & Tropical Medicine HPRO, Charahi-e-Shahid, Shar-e-Naw, Kabul, Afghanistan Email (Afg): amy.mikhail@googlemail.com Email (UK): Amy.Mikhail@lshtm.ac.uk Tel (Afg): +93 (0)706 126627 Tel (UK): +44 (0)781 4176107 [[alternative HTML version deleted]]