I didn't sort out the issue in  my email below but here is a (not very
R'ish) solution:

> pm = function(x) {
+   for(i in 1:7){
+      if(format(as.Date(Sys.Date()-i),'%w') == 1){
+      d = Sys.Date() - i;
+     }
+   }
+   d
+ }
> pm(Sys.Date())
[1] "2011-08-29"

On Fri, Sep 2, 2011 at 9:35 AM, Ben qant <ccqu...@gmail.com> wrote:

> Hello,
>
> I'm attempting to return the date (in form '%Y-%m-%d') of the Monday
> previous to the current date. For example: since it is 2011-09-02 today, I
> would expect 2011-08-29 to be the return value.
>
> I found the following in:
> http://www.mail-archive.com/r-help@r-project.org/msg144184.html
>
> Start quote from link:
> prevmonday <- function(x) 7 * floor(as.numeric(x-1+4) / 7) + as.Date(1-4)
>
> For example,
>
> > prevmonday(Sys.Date())
> [1] "2011-08-15"
> > prevmonday(prevmonday(Sys.Date()))
> [1] "2011-08-15"
>
> End quote from link.
>
> But when I do it I get:
> > prevmonday <- function(x) 7 * floor(as.numeric(x-1+4) / 7) + as.Date(1-4)
> > prevmonday(Sys.Date())
> Error in as.Date.numeric(1 - 4) : 'origin' must be supplied
>
> I've tried setting the 'origin' argument in as.Date() in different ways,
> but it returns inaccurate results.
>
> Thanks,
>
> Ben
>

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to