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

arun smartpink111 at yahoo.com
Sun Nov 25 02:29:23 CET 2012


HI,
If you need to order the dates.

dat1<-read.table(text="
site,date,precipitation,temp_max,temp_min
Castle Peak,January-70,0,32,18
Castle Peak,January-70,0,39,9
Castle Peak,September-70,0,34,5
Castle Peak,September-70,0,30,7
Castle Peak,October-70,0,40,6
Castle Peak,November-70,0,45,10
Castle Peak,December-70,0,43,8
Castle Peak,January-71,0,42,6
Castle Peak,February-71,0,38,5
CastlePeak,March-71,0,46,10
Castle Peak,October-71,0,42,7
Castle Peak,November-71,0,46,11
Castle Peak,December-71,0,41,9
",sep=",",stringsAsFactors=FALSE,header=TRUE)
Month1<-c("September","October","November","December")

 dat1$date[gsub("(.*)\\-.*","\\1",dat1$date)%in%Month1]<-paste0(gsub("(.*\\-).*","\\1",dat1$date[gsub("(.*)\\-.*","\\1",dat1$date)%in%Month1]),as.numeric(gsub(".*\\-(.*)","\\1",dat1$date[gsub("(.*)\\-.*","\\1",dat1$date)%in%Month1]))+1)

library(zoo)
dat1$date<-as.Date(as.yearmon(dat1$date,"%B-%y"),format="%b %Y")
dat2<-dat1[order(dat1$date),]
dat2$date<-as.yearmon(dat2$date,format="%Y-%m-%d")
row.names(dat2)<-1:nrow(dat2)
 dat2
#          site     date precipitation temp_max temp_min
#1  Castle Peak Jan 1970             0       32       18
#2  Castle Peak Jan 1970             0       39        9
#3  Castle Peak Jan 1971             0       42        6
#4  Castle Peak Feb 1971             0       38        5
#5   CastlePeak Mar 1971             0       46       10
#6  Castle Peak Sep 1971             0       34        5
#7  Castle Peak Sep 1971             0       30        7
#8  Castle Peak Oct 1971             0       40        6
#9  Castle Peak Nov 1971             0       45       10
#10 Castle Peak Dec 1971             0       43        8
#11 Castle Peak Oct 1972             0       42        7
#12 Castle Peak Nov 1972             0       46       11
#13 Castle Peak Dec 1972             0       41        9

A.K.

----- Original Message -----
From: nick pardikes <npardikes at hotmail.com>
To: "r-help at R-project.org" <r-help at r-project.org>
Cc: 
Sent: Saturday, November 24, 2012 4:01 PM
Subject: [R] Designating a new year (Sept-Aug) in R

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