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 <arun.kumar.saha at gmail.com>
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]]
>
> ______________________________________________
> 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.
>