Function to get a sequence of months

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Function to get a sequence of months

Arun.stat
Hi all,

I am looking for a function for following calculation.

start.month = "July"
end.month = "January"

months = f(start.month, end.month, by=1)

* f is the function that I am looking for.

Actually I want to get months = c("July", "August",.............."January")

If start.month = 6 and end.month = 1 then I could use (not properly) seq()
function and then I would get month as a vector with elements 6,5,4,3,2, and
1 by choosing "by=-1". Is there any function which can subsitute the seq()
function in my case?

Regards,

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Function to get a sequence of months

Duncan Murdoch
On 11/09/2007 6:36 AM, Arun Kumar Saha wrote:

> Hi all,
>
> I am looking for a function for following calculation.
>
> start.month = "July"
> end.month = "January"
>
> months = f(start.month, end.month, by=1)
>
> * f is the function that I am looking for.
>
> Actually I want to get months = c("July", "August",.............."January")
>
> If start.month = 6 and end.month = 1 then I could use (not properly) seq()
> function and then I would get month as a vector with elements 6,5,4,3,2, and
> 1 by choosing "by=-1". Is there any function which can subsitute the seq()
> function in my case?

I don't think one already exists, but it's easy to write one:

 > cyclic_seq <- function(from, to, cycle=12) {
+     if (to < from) (from - 1):(to + cycle - 1) %% cycle + 1
+     else from:to
+ }
 > cyclic_seq(5, 1)
[1]  5  6  7  8  9 10 11 12  1
 > cyclic_seq(5, 5)
[1] 5
 > cyclic_seq(5, 6)
[1] 5 6

This makes various assumptions about the inputs (e.g. it would fail on
cyclic_seq(20, 1) ); you might want to validate the inputs if you're
giving it to other people.

Duncan Murdoch

______________________________________________
[hidden email] 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 | Threaded
Open this post in threaded view
|

Re: Function to get a sequence of months

wl2776
Duncan Murdoch-2 wrote
On 11/09/2007 6:36 AM, Arun Kumar Saha wrote:
> Hi all,
>
> I am looking for a function for following calculation.
>
> start.month = "July"
> end.month = "January"
>
> months = f(start.month, end.month, by=1)
>
> * f is the function that I am looking for.
>
> Actually I want to get months = c("July", "August",.............."January")
>
> If start.month = 6 and end.month = 1 then I could use (not properly) seq()
> function and then I would get month as a vector with elements 6,5,4,3,2, and
> 1 by choosing "by=-1". Is there any function which can subsitute the seq()
> function in my case?

I don't think one already exists, but it's easy to write one:

 > cyclic_seq <- function(from, to, cycle=12) {
+     if (to < from) (from - 1):(to + cycle - 1) %% cycle + 1
+     else from:to
+ }
 > cyclic_seq(5, 1)
[1]  5  6  7  8  9 10 11 12  1
 > cyclic_seq(5, 5)
[1] 5
 > cyclic_seq(5, 6)
[1] 5 6

This makes various assumptions about the inputs (e.g. it would fail on
cyclic_seq(20, 1) ); you might want to validate the inputs if you're
giving it to other people.

Duncan Murdoch
And the rest would be to use month.name or month.abb variables

month.name[cyclic_seq(7,1)]
Reply | Threaded
Open this post in threaded view
|

Re: Function to get a sequence of months

Gabor Grothendieck
In reply to this post by Arun.stat
Here is a solution using character manipulation.  Noting that month.name
is built into R we paste together the character string:
"January Februrary ... December January ... December"
Then we use perl style ungreedy matching to get the shortest substring
matching the indicated expression then splitting it back apart and taking
the first match:

library(gsubfn)
strapply(paste(rep(month.name, 2), collapse = " "), "July.*?January",
  ~ strsplit(x, split = " "), perl = TRUE, simplify = c)[[1]]


Here is the result of running it:
> library(gsubfn)
> strapply(paste(rep(month.name, 2), collapse = " "), "July.*?January",
+   ~ strsplit(x, split = " "), perl = TRUE, simplify = c)[[1]]
[1] "July"      "August"    "September" "October"   "November"
"December"  "January"


mn2 <- c(month.name, month.name)
strapply(paste(mn2, "July.*January", ~ strsplit(x, split = " "),
simplify = unlist)

On 9/11/07, Arun Kumar Saha <[hidden email]> wrote:

> Hi all,
>
> I am looking for a function for following calculation.
>
> start.month = "July"
> end.month = "January"
>
> months = f(start.month, end.month, by=1)
>
> * f is the function that I am looking for.
>
> Actually I want to get months = c("July", "August",.............."January")
>
> If start.month = 6 and end.month = 1 then I could use (not properly) seq()
> function and then I would get month as a vector with elements 6,5,4,3,2, and
> 1 by choosing "by=-1". Is there any function which can subsitute the seq()
> function in my case?
>
> Regards,
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.