How can I group days into weeks?

前端 未结 3 2246
猫巷女王i
猫巷女王i 2020-12-17 18:39

In R, how can I associate between a vector of dates (days) and weeks?

Edit: \"weeks\" should be the week index within the date range and not within the year. My apol

3条回答
  •  -上瘾入骨i
    2020-12-17 19:24

    strftime is your friend...

    ? strftime # to see the help and the format
    
    set.seed(1)
    dates <- Sys.Date() + sample(1:365, size = 10)
    
    R> dates
     [1] "2012-02-11" "2012-03-21" "2012-06-01" "2012-09-30"
     [5] "2012-01-18" "2012-09-25" "2012-10-11" "2012-06-30"
     [9] "2012-06-18" "2011-11-28"
    
    # %j for julian day - number of the day since the 1st of january each year
    
    R> strftime(dates, format = "%j") # or format(dates, format = "%j")
     [1] "042" "081" "153" "274" "018" "269" "285" "182" "170" "332"
    
    R> strftime(dates, format = "%w")
     [1] "6" "3" "5" "0" "3" "2" "4" "6" "1" "1"
    
    # my locale is in French so...
    R> strftime(dates, format = "%A")
     [1] "samedi"   "mercredi" "vendredi" "dimanche" "mercredi"
     [6] "mardi"    "jeudi"    "samedi"   "lundi"    "lundi" 
    

    By the way what do you mean by day, day within the month, the week or the year ?

提交回复
热议问题