[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 sitedateprecipitation temp_max temp_min 1 Castle Peak January-70 0 32 18 2 Castle Peak January-70 0 399 3 Castle Peak January-70 0 345 4 Castle Peak January-70 0 307 5 Castle Peak January-70 0 406 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@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.
Re: [R] Designating a new year (Sept-Aug) in R
Hi, Try this: 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,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) dat1 # 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 September-71 0 34 5 #4 Castle Peak September-71 0 30 7 #5 Castle Peak October-71 0 40 6 #6 Castle Peak November-71 0 45 10 #7 Castle Peak December-71 0 43 8 #8 Castle Peak October-72 0 42 7 #9 Castle Peak November-72 0 46 11 #10 Castle Peak December-72 0 41 9 A.K. A.K. - Original Message - From: nick pardikes npardi...@hotmail.com To: r-help@R-project.org r-help@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@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. __ 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.
Re: [R] Designating a new year (Sept-Aug) in R
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 sitedateprecipitation temp_max temp_min 1 Castle Peak January-70 0 32 18 2 Castle Peak January-70 0 399 3 Castle Peak January-70 0 345 4 Castle Peak January-70 0 307 5 Castle Peak January-70 0 406 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@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. __ 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.
Re: [R] Designating a new year (Sept-Aug) in R
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 npardi...@hotmail.com To: r-help@R-project.org r-help@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@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. __ 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.