Re: [R] get start and end date of ISO weeks giving a date as input
Hello Enrico, 2016-09-08 10:41 GMT-03:00 Enrico Schumann : > Hi Veronica, > > please see inline. > > On Thu, 08 Sep 2016, Veronica Andreo writes: > > > Hello Luisfo and Enrico, > > > > Thanks for your help! I've been testing both > > solutions... results differ for the same date (I > > changed both functions to use ISO8601). And I added > > contiguous dates, to see how they handle the > > start-end of the week. > > > > So, here the results: > > > > ### one example > > d <- c("2010-08-21","2010-08-22","2010-08-23","2010-08-24") > > iso_start_end <- function(d) { > > d <- as.Date(d) > > wday <- as.POSIXlt(d)$wday > > data.frame(date = d, > > week = format(d, "%V"), > > starts = d - wday + 1, > > ends = d + 7 - wday) > > } > > iso_start_end(d) > > > > date week starts ends > > 1 2010-08-21 33 2010-08-16 2010-08-22 > > 2 2010-08-22 33 2010-08-23 2010-08-29 > > 3 2010-08-23 34 2010-08-23 2010-08-29 > > 4 2010-08-24 34 2010-08-23 2010-08-29 > > Yes, the second date makes no sense, and it happens > because Sunday is 0 (and not 7). My bad. Here is > a fixed version: > > iso_start_end <- function(d) { > d <- as.Date(d) > wday <- as.POSIXlt(d)$wday > wday[wday == 0] <- 7 > data.frame(date = d, > week = format(d, "%V"), > starts = d - wday + 1, > ends = d + 7 - wday) > } > > > > ### the other example: > > dd <- as.Date(strptime('2010-08-21', format="%Y-%m-%d", tz="GMT")) > > ref.date <- as.Date(strptime(paste0(year(dd),"-01-01"), > format="%Y-%m-%d")) > > bound.dates <- ref.date + 7 * (isoweek(dd)) + c(0,6) > > bound.dates > > [1] "2010-08-20" "2010-08-26" > > You can use the function "weekdays" to see check the > results. > > > weekdays(bound.dates) > [1] "Friday" "Thursday" > > > So, researching a bit more and inspired by those > > examples, I eventually came up with this solution > > that seems to work fine... I share in case that any > > other has a similar problem: > > > > # get ISOweek for my vector of dates > > week_iso<-ISOweek(d) > > > > # vector with the format %Y-W%V-1 for start day of the ISO week > > week_iso_day1 <- paste(week_iso,1, sep="-") > > > > # vector with the format %Y-W%V-7 for end day of the ISO week > > week_iso_day7 <- paste(week_iso, 7, sep="-") > > > > # use ISOweek2date > > data.frame(date= d, week_iso = week_iso, start = > ISOweek2date(week_iso_day1), end = ISOweek2date(week_iso_day7) > > > > date week_iso startend > > 1 2010-08-21 2010-W33 2010-08-16 2010-08-22 > > 2 2010-08-22 2010-W33 2010-08-16 2010-08-22 > > 3 2010-08-23 2010-W34 2010-08-23 2010-08-29 > > 4 2010-08-24 2010-W34 2010-08-23 2010-08-29 > > The updated 'iso_start_end' gives the same result. > > date week starts ends > 1 2010-08-21 33 2010-08-16 2010-08-22 > 2 2010-08-22 33 2010-08-16 2010-08-22 > 3 2010-08-23 34 2010-08-23 2010-08-29 > 4 2010-08-24 34 2010-08-23 2010-08-29 > Yes! Again, thanks for your time and help! Best, Vero [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] get start and end date of ISO weeks giving a date as input
Hello Luisfo and Enrico, Thanks for your help! I've been testing both solutions... results differ for the same date (I changed both functions to use ISO8601). And I added contiguous dates, to see how they handle the start-end of the week. So, here the results: ### one example d <- c("2010-08-21","2010-08-22","2010-08-23","2010-08-24") iso_start_end <- function(d) { d <- as.Date(d) wday <- as.POSIXlt(d)$wday data.frame(date = d, week = format(d, "%V"), starts = d - wday + 1, ends = d + 7 - wday) } iso_start_end(d) date week starts ends 1 2010-08-21 33 2010-08-16 2010-08-22 *2 2010-08-22 33 2010-08-23 2010-08-29* 3 2010-08-23 34 2010-08-23 2010-08-29 4 2010-08-24 34 2010-08-23 2010-08-29 ### the other example: dd <- as.Date(strptime('2010-08-21', format="%Y-%m-%d", tz="GMT")) ref.date <- as.Date(strptime(paste0(year(dd),"-01-01"), format="%Y-%m-%d")) bound.dates <- ref.date + 7 * (isoweek(dd)) + c(0,6) bound.dates [1] "2010-08-20" "2010-08-26" So, researching a bit more and inspired by those examples, I eventually came up with this solution that seems to work fine... I share in case that any other has a similar problem: # get ISOweek for my vector of dates week_iso<-ISOweek(d) # vector with the format %Y-W%V-1 for start day of the ISO week week_iso_day1 <- paste(week_iso,1, sep="-") # vector with the format %Y-W%V-7 for end day of the ISO week week_iso_day7 <- paste(week_iso, 7, sep="-") # use ISOweek2date data.frame(date= d, week_iso = week_iso, start = ISOweek2date(week_iso_day1), end = ISOweek2date(week_iso_day7) date week_iso startend 1 2010-08-21 2010-W33 2010-08-16 2010-08-22 2 2010-08-22 2010-W33 2010-08-16 2010-08-22 3 2010-08-23 2010-W34 2010-08-23 2010-08-29 4 2010-08-24 2010-W34 2010-08-23 2010-08-29 Thanks again for your time, ideas and help! Best, Vero 2016-09-08 8:20 GMT-03:00 Luisfo : > Dear Veronica, > > Here there's a way of doing what you requested. > > library("lubridate") > # your date '2010-08-21' as Date object > dd <- as.Date(strptime("2010-08-21", format="%Y-%m-%d", tz="GMT")) > # take the first day of the year as Date object, i.e. 2010-01-01 in our > example > ref.date <- as.Date(strptime(paste0(year(dd),"-01-01"), > format="%Y-%m-%d", tz="GMT")) > # the start and end dates > bound.dates <- ref.date + 7 * (week(dd)-1) + c(0,6) > > I hope you find it useful. > > Best, > *Luisfo Chiroque* > > *PhD Student | PhD Candidate IMDEA Networks Institute* > http://fourier.networks.imdea.org/people/~luis_nunez/ > > On 09/08/2016 12:13 PM, Veronica Andreo wrote: > > Hello list, > > Is there a quick way to get start and end date (%Y-%m-%d) from ISO > weeks if I only have dates? > > For example, I have this date in which some event happened: > "2010-08-21". Not only I want the ISO week, which I can obtain either > with isoweek (lubridate) or ISOweek (ISOweek), but I want the start > and end date of that ISO week. > > Do I need to print all ISO weeks from the period of interest and > sample there for start and end date? Or is there a better way to do > that? > > Thanks a lot in advance! > > Best, > Veronica > > __r-h...@r-project.org mailing > list -- To UNSUBSCRIBE and more, > seehttps://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. > > > [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] get start and end date of ISO weeks giving a date as input
Hello list, Is there a quick way to get start and end date (%Y-%m-%d) from ISO weeks if I only have dates? For example, I have this date in which some event happened: "2010-08-21". Not only I want the ISO week, which I can obtain either with isoweek (lubridate) or ISOweek (ISOweek), but I want the start and end date of that ISO week. Do I need to print all ISO weeks from the period of interest and sample there for start and end date? Or is there a better way to do that? Thanks a lot in advance! Best, Veronica __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] multiple model selection for vglm models
Hi list, I'm working with vglm (family tobit) models and I need to perform multiple model selection. Something similar to step or stepAIC, is there anything alike? I found that neither step nor stepAIC work for objects of class vglm. Has anybody worked with them and can give a hint on how to select among vglm models? Thanks a lot in advance, Vero [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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] help with script to get starting date of blooms
Hi Jim 2014-07-19 8:25 GMT-03:00 Jim Lemon : > > Hi Vero, > I think this does what you want, even though it doesn't do it the way > you describe above. > > cla<-runif(506)+ > rep(c(seq(1,3,length.out=23),seq(3,1,length.out=23)),11) > obs_time<-paste(rep(1:11,each=46),rep(1:46,11),sep="_") > t_max<-rep(0,506) > t_begin<-rep("",11) > for(year in 1:11) { > threshold<-median(cla[(year-1) * 46 + 1:46]) * 1.05 > max_pos<-(year-1) * 46 + which.max(cla[(year-1)*46+1:46]) > t_max[max_pos]<-1 > threshpos<-(year-1) * 46 + 1 > while(cla[threshpos] < threshold) threshpos <- threshpos + 1 > cat(threshold,cla[threshpos],max_pos,cla[max_pos],"\n") > t_begin[year]<-obs_time[threshpos] > } > Yes, indeed!!! It does the job beautifully!!! Thanks so much! I'll now try to adapt it to the 150.000 time series that form my study area in South Atlantic Ocean :) Any advice? Best, Vero [[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 with script to get starting date of blooms
Hi list I have a vector, which are remotely sensed chlorophyll values for a certain pixel in 11 years, then i have a flag or label vector (t_max) that consists of 0 and 1, which tells me where i have the annual peak (one per year, then eleven 1 and all the rest are 0). I'm interested in extracting the date in which the bloom beggins. For that matter I'm using a threshold of 5% above the median of the whole series. I need to go over the chlorophyll data vector (cla) and every time I find the yearly maximum value (where t_max == 1), i need to go backwards in cla as many time steps as needed untill I cross the threshold (5% over the median of cl)... once i crossed that threshold, i need to be sure that at least for 2 time steps, the value of cla keeps going down (it's below the threshold)... once that condition is met, i want to get the first position where cla is higher than that threshold... that would be the phytoplankton bloom starting date... This is more or less what i was trying, but it does not work and i'm stuck... can you please help me?? cla<- ts of 506 values (every 46 values, i have one year) t_max<- vector of 0 & 1; 1 where the yearly cla max occurs (11 ones & all rest 0) threshold = 0.05*median(cla)+median(cla) for ( t in 1:length(t_max) ) { if ( t_max[t] == 0 ) { next; } else { if ( (cla[t-1] < threshold) && (cla[t-2)] < threshold) ) { return which(cla[t]) } } } Thanks a lot in advance!! :) Vero [[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.