[R] changing the day of the week in dates format

jim holtman jholtman at gmail.com
Sun May 15 23:46:02 CEST 2011


What is it that you want to do?  If you move the dates forward a year,
then what does it mean to add one year to 2/29/2008?  You did mention
accounting for leap year.  It goes the other way with 2/28/2007 and
3/1/2007; what is your expectation in these cases?  You can always
convert everything to characters and then substring out the year and
put the new one in, and then check for the leap year condition and do
the appropriate action.

The equation that you used would add 6 hours to each each succeeding
year's date.

So I ask my favorite question:  "what is the problem that you are
trying to solve?"

On Sun, May 15, 2011 at 11:13 AM, Dave Evens <daveevens1 at yahoo.co.uk> wrote:
> Hi Adrian,
>
> Many thanks for your reply.
>
> Suppose I wanted to increment the date by a year - how would I account for things like leap years?
>
> Would I just do
>> mydaysx[select] <- mydaysx[select] + 365.25*24*60*60
>
> Regards,Dave
>
>
> ________________________________
> From: Adrian Duffner <duffnera at googlemail.com>
>
> Cc: "r-help at r-project.org" <r-help at r-project.org>
> Sent: Sunday, 15 May 2011, 14:21
> Subject: Re: [R] changing the day of the week in dates format
>
> Hi Dave,
>
> your problem is that you are working with a S3 class, what is mainly a
> list with naming convention. Hence it is possible to change just one
> entry of the list, but it is nearly never recommendable.
>
> So a slight change to your code should provide you the required output:
>> mydaysx[select] <- mydaysx[select] + 2*24*60*60
>> select <- mydaysx$wday==6
>> sum(select)
> [1] 0
>
> In this case not only the entry $mday of the list is changed, but the
> whole object is updated.
>
> Cheers
> Adrian
>
> Am 14.05.2011 20:44, schrieb Dave Evens:
>> Dear all,
>>
>> I have a question related to the POSIXlt function in R.
>>
>> I have a set of dates and times, for exmaple:
>>
>> startx<- as.POSIXct("2011-01-01 00:00:00")
>> finx<- as.POSIXct("2011-12-31 00:00:00")
>>
>> daysx<- seq(startx, finx, by="24 hours")
>>
>> I
>>   want to change the dates of all the days falling on a Saturday to the
>> next working day (i.e. Monday). So I convert dates to POSIXlt
>>
>> mydaysx<- as.POSIXlt(daysx)
>>
>> Then I change select all the Saturday's and move them on to Monday
>>
>> select<- mydaysx$wday==6
>> mydaysx$mday[select]<- mydaysx$mday[select] + 2
>>
>> However,
>>   although all the new dates (i.e. mydaysx) are actual days of the year -
>>   the $wday have not been updated and the $mdays have not all been
>> corrected (i.e. those falling into the next month). So if I do
>>
>> select<- mydaysx$wday==6
>>
>> I still get the same set of days as before.
>>
>> Is there a way to do this?
>>
>> Thanks,
>>
>>     [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> 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.
>
>



-- 
Jim Holtman
Data Munger Guru

What is the problem that you are trying to solve?



More information about the R-help mailing list