Hello R-users, Does anyone know how obtain the week of a date? (in SPPS the instruction is "xdate.week") Thanks, Juan Ramon -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
I am not quite sure what the "week of a date" is, but here is my best guess: > format(as.POSIXct("2002-04-11"), "%U") > 14 This returns a number between 0 and 53 representing the week-of-the-year. Check the manpages for the date-related functions. Andy __________________________________ Andy Jaworski Engineering Systems Technology Center 3M Center, 518-1-01 St. Paul, MN 55144-1000 ----- E-mail: apjaworski at mmm.com Tel: (651) 733-6092 Fax: (651) 736-3122 "Juan Ramon Gonzalez" To: r-help at stat.math.ethz.ch <jrgonzalez at ico cc: (bcc: Andrzej P. Jaworski/US-Corporate/3M/US) .scs.es> Subject: [R] extract week from date 04/11/2002 08:42 Hello R-users, Does anyone know how obtain the week of a date? (in SPPS the instruction is "xdate.week") Thanks, Juan Ramon -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Juan Ramon Gonzalez wrote:>Hello R-users, > >Does anyone know how obtain the week of a date? >(in SPPS the instruction is "xdate.week") > >Thanks, > >Juan Ramon > >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- >r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html >Send "info", "help", or "[un]subscribe" >(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch >_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >weekday<-format(x, "%w") will change the date x into week as decimal number (0-6, Sunday is 0), or week<-format(x, "%U%) week of the year as decimal number (00-53) using the first Sunday as day 1 of week 1, or week<-format(x, "%W") week of the year as decimal number (00-53) using the first Monday as day 1 of week 1. See help.start("strptime"). Pingping Zheng -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"Juan Ramon Gonzalez" <jrgonzalez at ico.scs.es> writes:> Does anyone know how obtain the week of a date? > (in SPPS the instruction is "xdate.week")If you convert a date to a POSIXlt class, say by using the strptime function, you get a list with components `sec' 0-61: seconds `min' 0-59: minutes `hour' 0-23: hours `mday' 1-31: day of the month `mon' 0-11: months after the first of the year. `year' Years since 1900. `wday' 0-6 day of the week, starting on Sunday. `yday' 0-365: day of the year. `isdst' Daylight savings time flag. Positive if in force, zero if not, negative if unknown. You should be able to compute the week of the year from yday. I think it would be ceiling(dd$yday/7 + 0.5) but you should check this. For example> as.character(unique(cheese[,1]))[1] "5/4/99" "5/5/99" "5/7/99" "5/11/99" "5/12/99" "5/14/99" "5/18/99" [8] "5/19/99" "5/21/99" "5/25/99" "5/27/99" "5/28/99" "8/3/99" "8/4/99" [15] "8/6/99" "8/10/99" "8/11/99" "8/12/99" "8/13/99" "8/17/99" "8/18/99" [22] "8/19/99" "8/20/99" "8/24/99" "8/25/99" "8/27/99" "8/31/99"> dd <- strptime(as.character(unique(cheese[,1])), format = '%m/%d/%y') > ceiling(dd$yday/7)[1] 18 18 18 19 19 19 20 20 20 21 21 21 31 31 31 32 32 32 32 33 33 33 33 34 34 [26] 34 35 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Thu, 11 Apr 2002, Juan Ramon Gonzalez wrote:> Hello R-users, > > Does anyone know how obtain the week of a date? > (in SPPS the instruction is "xdate.week")Extract the day and divide by seven? as.POSIXlt(a.date)$yday %/% 7 Perhaps add 1 if you want the year to start at week 1 rather than week 0. -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
this is more complicated. the definition of week 1 is noct clear. but as far as i know the week containing the first of january counts as last week if at least 4 of its days belong to the old year, and it is the first week if at least 4 of its days belong to the new year. i do not know if there is a standardized definiton with regard to the fact that the week starts on sunday in soem countries (us?) and on monday in some other countries (most european countries?)>>Does anyone know how obtain the week of a date? >>(in SPPS the instruction is "xdate.week") >> > > Extract the day and divide by seven? > > as.POSIXlt(a.date)$yday %/% 7 >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
from the calendar faq 5.7. What is the week number? ----------------------------- International standard ISO-8601 (mentioned in section 5.6) assigns a number to each week of the year. A week that lies partly in one year and partly in another is assigned a number in the year in which most of its days lie. This means that Week 1 of any year is the week that contains 4 January, or equivalently Week 1 of any year is the week that contains the first Thursday in January. Most years have 52 weeks, but years that start on a Thursday and leap years that start on a Wednesday have 53 weeks. Note: This week numbering system is not used in the United States. -=-=-=-=- can somebody from the united states tell us how weeks are numbered there? -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._