tag:blogger.com,1999:blog-1901659770749170850.post1980883321432394713..comments2018-11-27T06:39:27.023-05:00Comments on Quantitative Ecology: ISO weekForesterhttp://www.blogger.com/profile/00291122844817714304noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-1901659770749170850.post-39516424767136459232010-05-19T08:43:31.348-04:002010-05-19T08:43:31.348-04:00## Inputs a date object,
##posix object, or 3 num...## Inputs a date object, <br />##posix object, or 3 numbers and<br />##gives back the iso week.<br />## By Gustaf Rydevik, revised 2010<br /><br /><br />getweek<-function(Y,M=NULL,D=NULL){<br /> <br /> if(!class(Y)[1]%in% c("Date","POSIXt")) {<br /> date.posix<-strptime(paste(Y,M,D,sep="-"),"%Y-%m-%d")<br /> }<br /> if(class(Y)[1]%in% c("POSIXt","Date")){<br />date.posix<-as.POSIXlt(Y)<br />Y<-as.numeric(format(date.posix,<br />"%Y"))<br />M<-as.numeric(format(date.posix,<br />"%m"))<br />D<-as.numeric(format(date.posix,<br />"%d"))<br /> }<br /><br /><br /> LY<- (Y%%4==0 & !(Y%%100==0))|(Y%%400==0)<br /> LY.prev<- ((Y-1)%%4==0 & !((Y-1)%%100==0))|((Y-1)%%400==0)<br /> date.yday<-date.posix$yday+1<br /> jan1.wday<-strptime(paste(Y,"01-01"<br />,sep="-"),"%Y-%m-%d")$wday<br /> jan1.wday<-ifelse(jan1.wday==0,7,<br />jan1.wday)<br /> date.wday<-date.posix$wday<br /> date.wday<-ifelse(date.wday==0,7,<br />date.wday)<br /> <br /> <br />####If the date is in the <br />####beginning, or end of the year,<br />### does it fall into a week of <br />###the previous or next year?<br /><br /> Yn<-ifelse(date.yday<=(8-jan1.wday)&jan1.wday>4,Y-1,<br /> ifelse(((365+LY-date.yday)<(4-date.wday)),Y+1,Y))<br /> <br />##Set the week differently if<br />##the date is in the <br />##beginning,middle or end of the <br />##year<br /> <br /> Wn<-ifelse(<br /> Yn==Y-1,<br /> ifelse((jan1.wday==5|(jan1.wday==6 &LY.prev)),53,52),<br /> ifelse(Yn==Y+1,1,(date.yday+(7-date.wday)+(jan1.wday-1))/7-(jan1.wday>4))<br /> )<br /><br /> return(list(Year=Yn,ISOWeek=Wn))<br />}G.https://www.blogger.com/profile/03031149641420611283noreply@blogger.comtag:blogger.com,1999:blog-1901659770749170850.post-51429075029002749452010-05-19T08:39:58.937-04:002010-05-19T08:39:58.937-04:00Hi,
Interesting to see that someone have found my ...Hi,<br />Interesting to see that someone have found my code! And interesting to find that coding errors come back to haunt you - you're right of course - the code I posted in dec 2008 was a bit buggy - I discovered this myself when 2010 turned around. I'll include my corrected version in the next comment.<br /><br />Best Regards,<br />GustafG.https://www.blogger.com/profile/03031149641420611283noreply@blogger.comtag:blogger.com,1999:blog-1901659770749170850.post-2930346056043162802010-04-26T14:47:21.923-04:002010-04-26T14:47:21.923-04:00A useful function is match.arg, so that you can re...A useful function is match.arg, so that you can replace<br /><br />return.val=c("week","day") in the arguments<br /><br />and inside the function<br /><br />return.val <- match.arg(return.val)<br /><br />Also, you can replace<br /><br />print("some error")<br />break<br /><br />with<br /><br />stop("Some error")<br /><br />I enjoyed your posts.<br /><br />Thanks!<br /><br />FernandoUnknownhttps://www.blogger.com/profile/00341662602446944238noreply@blogger.comtag:blogger.com,1999:blog-1901659770749170850.post-72365281956966449302010-02-19T11:32:43.179-05:002010-02-19T11:32:43.179-05:00Good idea Ajay.Good idea Ajay.Foresterhttps://www.blogger.com/profile/00291122844817714304noreply@blogger.comtag:blogger.com,1999:blog-1901659770749170850.post-90198750253393832592010-02-19T10:57:02.259-05:002010-02-19T10:57:02.259-05:00Thanks for this - it works well for me. There is a...Thanks for this - it works well for me. There is a base function called ISOdate so I wonder if you would call it something like ISOweek?Ajay Ozahttps://www.blogger.com/profile/05822078499158381056noreply@blogger.comtag:blogger.com,1999:blog-1901659770749170850.post-54653103294887627652009-12-02T10:56:28.107-05:002009-12-02T10:56:28.107-05:00Hi Ben. Thanks for your comments.
(1) You are rig...Hi Ben. Thanks for your comments.<br /><br />(1) You are right -- I also ran into the POSIXct problem but forgot to update my posted code. Now I have edited the script so that it works (by converting "ct" to "lt") -- it is a crude fix, but it will do until I figure out a better way to handle all the different time formats.<br /><br />(2) In this case the processing time is not a big deal, but I am always interested in learning more efficient coding methods that I can translate to other, more time sensitive, processes. Converting dates is not the bottleneck of my current project, so it is not worth spending a lot of time on; however, I will play around with the strptime() calls when I get a chance.Foresterhttps://www.blogger.com/profile/00291122844817714304noreply@blogger.comtag:blogger.com,1999:blog-1901659770749170850.post-7092380744116129922009-11-29T15:19:04.936-05:002009-11-29T15:19:04.936-05:002 brief comments:
(1) it really wants POSIXlt (ra...2 brief comments:<br /><br />(1) it really wants POSIXlt (rather than POSIXct -- just any old "POSIXt" object won't do)<br /><br />(2) from profiling, it looks like the strptime() call is taking most of the time. I don't know if you can combine the two strptime() calls, or whether that will help much, or what the alternative code does to avoid strptime() calls. (It took me about 20 seconds to convert 10^5 date values. In the big picture, is that worth worrying about?)bbolkerhttps://www.blogger.com/profile/01968803538554694988noreply@blogger.com