[R] Designating a new year (Sept-Aug) in R

Rui Barradas ruipbarradas at sapo.pt
Sun Nov 25 00:01:56 CET 2012


Hello,

I don't know if this is it, do you want a 4 digit year or month-yy? The 
following function returns month-yy.


fun <- function(x, format = "%B-%y"){
     fmt <- format
     x <- as.Date(paste("01", x, sep = "-"), format = paste("%d", fmt, 
sep = "-"))
     m <- as.integer(format(x, "%m"))
     y <- as.integer(format(x, "%y"))
     y <- ifelse(9 <= m & m <= 12, y + 1, y)
     format(as.Date(paste(y, m, 1), "%y %m %d"), fmt)
}

x <- Sys.Date() + 30*(-12:12)
x <- format(x, "%B-%y")

fun(x)

I don't believe it's hard to change to 4 digit year, just change the 
return format to "%Y" (last line of code).

Hope this helps,

Rui Barradas
Em 24-11-2012 21:01, nick pardikes escreveu:
> If I have data (below) and need some help in figuring out how I can change the values of my date column, so that a year will be from September-August? So the year
> 1990 = September 89-August 90; 1991
>   = September 90-August 91, etc...
>
> I was trying to use the if() function, but am unable to figure it out. I basically need to change the years associated with September-December to the following year. Any
> help would be greatly appreciated. Otherwise I will have to power through
>   it and do it all manually in excel. I am sorry that I do not have the original data associated with this posting, nor any R code with it. I really do not have a clue how to even start designating the new year.
>
> head(mydata)
> class(mydata)
> "data.frame"
>           site            date                precipitation temp_max temp_min
> 1 Castle Peak January-70             0       32       18
> 2 Castle Peak January-70             0       39        9
> 3 Castle Peak January-70             0       34        5
> 4 Castle Peak January-70             0       30        7
> 5 Castle Peak January-70             0       40        6
> 6 Castle Peak January-70             0       45       10
>
> Thank you in advance and please let me know what else I can include to help solve this issue. this is my first posting on R-help.
>
>
>
> Nick Pardikes
> PhD Student
> Program in Ecology, Evolution and Conservation Biology
> University of Nevada, Reno
> 303-550-1072
> http://wolfweb.unr.edu/homepage/npardikes/MySite/Welcome.html
>   		 	   		
> 	[[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.




More information about the R-help mailing list