Dear R-users, I have a factor variable within my data frame which I derive week after week from a POSIXct variable using the cut(var,"weeks") command I have found in the chron package. The levels() command gives me: [1] "2009-03-30 00:00:00" "2009-04-06 00:00:00" "2009-04-13 00:00:00" "2009-04-20 00:00:00" "2009-04-27 00:00:00" "2009-05-04 00:00:00" "2009-05-11 00:00:00" "2009-05-18 00:00:00" [9] "2009-05-25 00:00:00" "2009-06-01 00:00:00" "2009-06-08 00:00:00" "2009-06-15 00:00:00" "2009-06-22 00:00:00" "2009-06-29 00:00:00" "2009-07-06 00:00:00" "2009-07-13 00:00:00" [17] "2009-07-20 00:00:00" "2009-07-27 00:00:00" "2009-08-03 00:00:00" "2009-08-10 00:00:00" "2009-08-17 00:00:00" "2009-08-24 00:00:00" "2009-08-31 00:00:00" "2009-09-07 00:00:00" [25] "2009-09-14 00:00:00" "2009-09-21 00:00:00" "2009-09-28 00:00:00" "2009-10-05 00:00:00" "2009-10-12 00:00:00" "2009-10-19 00:00:00" "2009-10-25 23:00:00" "2009-11-01 23:00:00" [33] "2009-11-08 23:00:00" "2009-11-15 23:00:00" "2009-11-22 23:00:00" "2009-11-29 23:00:00" "2009-12-06 23:00:00" "2009-12-13 23:00:00" "2009-12-20 23:00:00" "2009-12-27 23:00:00" [41] "2010-01-03 23:00:00" "2010-01-10 23:00:00" "2010-01-17 23:00:00" "2010-01-24 23:00:00" "2010-01-31 23:00:00" "2010-02-07 23:00:00" "2010-02-14 23:00:00" "2010-02-21 23:00:00" [49] "2010-02-28 23:00:00" "2010-03-07 23:00:00" "2010-03-14 23:00:00" "2010-03-21 23:00:00" "2010-03-29 00:00:00" "2010-04-05 00:00:00" "2010-04-12 00:00:00" "2010-04-19 00:00:00" [57] "2010-04-26 00:00:00" "2010-05-03 00:00:00" "2010-05-10 00:00:00" "2010-05-17 00:00:00" "2010-05-24 00:00:00" "2010-05-31 00:00:00" "2010-06-07 00:00:00" "2010-06-14 00:00:00" [65] "2010-06-21 00:00:00" "2010-06-28 00:00:00" "2010-07-05 00:00:00" "2010-07-12 00:00:00" "2010-07-19 00:00:00" "2010-07-26 00:00:00" "2010-08-02 00:00:00" "2010-08-09 00:00:00" [73] "2010-08-16 00:00:00" "2010-08-23 00:00:00" "2010-08-30 00:00:00" "2010-09-06 00:00:00" "2010-09-13 00:00:00" "2010-09-20 00:00:00" "2010-09-27 00:00:00" "2010-10-04 00:00:00" [81] "2010-10-11 00:00:00" "2010-10-18 00:00:00" "2010-10-25 00:00:00" "2010-10-31 23:00:00" "2010-11-07 23:00:00" "2010-11-14 23:00:00" "2010-11-21 23:00:00" "2010-11-28 23:00:00" [89] "2010-12-05 23:00:00" "2010-12-12 23:00:00" Now what I would like is to have more readable labels, such as 2010-W01 for the first week of 2010, 2009-W34 for the 34th week in 2009, etc....is there an easier way to achieve that than having to write out the all recode sintax: library(car) dataset$newvar <- recode(dataset$oldvar, " c('2009-03-30 00:00:00')='2009-W13'; c('2009-04-06 00:00:00')='2009-W14'; # etc... c('2010-12-05 23:00:00')='2009-W48'; c('2010-12-12 23:00:00')='2009-W49'; # etc...this part should be updated with time unless I'll find some automatic procedure ") Thanks, Luca Luca Meyer lucameyer.com IBM SPSS Statistics release 19.0.0 R version 2.12.1 (2010-12-16) Mac OS X 10.6.5 (10H574) - kernel Darwin 10.5.0
On Dec 17, 2010, at 11:08 AM, Luca Meyer wrote: x= factor(c("2009-03-30 00:00:00", "2009-04-06 00:00:00", "2009-04-13 00:00:00", "2009-04-20 00:00:00", "2009-04-27 00:00:00", "2009-05-04 00:00:00" ,"2009-05-11 00:00:00", "2009-05-18 00:00:00")) require(lubridate) xd=as.POSIXct(x) week(xd) # [1] 13 14 15 16 17 18 19 20 year(xd) # [1] 2009 2009 2009 2009 2009 2009 2009 2009 paste(year(xd), " W",week(xd), sep="") #[1] "2009 W13" "2009 W14" "2009 W15" "2009 W16" "2009 W17" "2009 W18" "2009 W19" "2009 W20" David Winsemius, MD West Hartford, CT
Something goes wrong with the week function of the lubridate package:> x= as.POSIXct(factor(c("2010-12-15 17:28:27",+ "2010-12-15 17:32:34", + "2010-12-15 18:48:39", + "2010-12-15 19:25:00", + "2010-12-16 08:00:00", + "2010-12-16 08:25:49", + "2010-12-16 09:00:00")))> require(lubridate) > weekdays(x)[1] "Mercoled?" "Mercoled?" "Mercoled?" "Mercoled?" "Gioved?" "Gioved?" "Gioved?"> week(x)[1] 50 50 50 50 51 51 51>Please notice Mercoled?=Wednesday and Gioved?=Thursday, why would the beginning of the week start on Thursday? Also please beware that on previous weeks this does not occur, that is all weeks till 49 will all begin on Mondays and end on Sundays as required. Thanks, Luca Il giorno 18/dic/2010, alle ore 14.39, David Winsemius ha scritto:> > On Dec 17, 2010, at 11:08 AM, Luca Meyer wrote: > > x= factor(c("2009-03-30 00:00:00", "2009-04-06 00:00:00", "2009-04-13 00:00:00", "2009-04-20 00:00:00", "2009-04-27 00:00:00", "2009-05-04 00:00:00" ,"2009-05-11 00:00:00", "2009-05-18 00:00:00")) > require(lubridate) > xd=as.POSIXct(x) > week(xd) > # [1] 13 14 15 16 17 18 19 20 > year(xd) > # [1] 2009 2009 2009 2009 2009 2009 2009 2009 > paste(year(xd), " W",week(xd), sep="") > #[1] "2009 W13" "2009 W14" "2009 W15" "2009 W16" "2009 W17" "2009 W18" "2009 W19" "2009 W20" > > > > David Winsemius, MD > West Hartford, CT >
On Dec 19, 2010, at 5:11 AM, Luca Meyer wrote:> Something goes wrong with the week function of the lubridate package: > >> x= as.POSIXct(factor(c("2010-12-15 17:28:27", > + "2010-12-15 17:32:34", > + "2010-12-15 18:48:39", > + "2010-12-15 19:25:00", > + "2010-12-16 08:00:00", > + "2010-12-16 08:25:49", > + "2010-12-16 09:00:00"))) >> require(lubridate)>> weekdays(x) > [1] "Mercoled?" "Mercoled?" "Mercoled?" "Mercoled?" "Gioved?" > "Gioved?" "Gioved?" >> week(x) > [1] 50 50 50 50 51 51 51But 2010-12-15 is a Wednesday and 2010-12-16 is a Thursday. -- David.>> > > Please notice Mercoled?=Wednesday and Gioved?=Thursday, why would > the beginning of the week start on Thursday? Also please beware that > on previous weeks this does not occur, that is all weeks till 49 > will all begin on Mondays and end on Sundays as required. > > Thanks, > Luca > > > Il giorno 18/dic/2010, alle ore 14.39, David Winsemius ha scritto: > >> >> On Dec 17, 2010, at 11:08 AM, Luca Meyer wrote: >> >> x= factor(c("2009-03-30 00:00:00", "2009-04-06 00:00:00", >> "2009-04-13 00:00:00", "2009-04-20 00:00:00", "2009-04-27 >> 00:00:00", "2009-05-04 00:00:00" ,"2009-05-11 00:00:00", >> "2009-05-18 00:00:00")) >> require(lubridate) >> xd=as.POSIXct(x) >> week(xd) >> # [1] 13 14 15 16 17 18 19 20 >> year(xd) >> # [1] 2009 2009 2009 2009 2009 2009 2009 2009 >> paste(year(xd), " W",week(xd), sep="") >> #[1] "2009 W13" "2009 W14" "2009 W15" "2009 W16" "2009 W17" "2009 >> W18" "2009 W19" "2009 W20" >> >> >> >> David Winsemius, MD >> West Hartford, CT >> > > ______________________________________________ > R-help at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.David Winsemius, MD West Hartford, CT
Maybe Matching Threads
- Strange regalloc behaviour: one more available register causes much worse allocation
- Strange regalloc behaviour: one more available register causes much worse allocation
- Hardware ASan Generating Unknown Instruction
- [LLVMdev] ScheduleDAGInstrs computes deps using IR Values that may be invalid
- R: Music oh hold