Fill a vector with weekdays only

后端 未结 2 1890
情深已故
情深已故 2020-12-20 22:05

I know the start date start and the last date maturity. How can I fill in a vector with dates without taking weekends dates into account ? For ins

相关标签:
2条回答
  • 2020-12-20 22:36

    This less human than @joran answer:) , but it is no local-time depending

    dd <- seq(as.Date('2011-01-01'),as.Date('2011-12-31'),by = 1)
    dd[! (as.POSIXlt(dd)$wd %in% c(0,1))]
    

    PS : another option , is to set locals before applying weekdays

    tt <- Sys.getlocale('LC_TIME')
    Sys.setlocale('LC_TIME','ENGLISH')
    dd <- dd[!weekdays(x) %in% c('Saturday','Sunday')]
    Sys.setlocale('LC_TIME',tt)
    
    0 讨论(0)
  • 2020-12-20 22:46

    There are probably a billion ways to do this with a variety of functions from multiple packages. But my first thought is to simply make a sequence and then remove the weekends:

    x <- seq(as.Date('2011-01-01'),as.Date('2011-12-31'),by = 1)
    x <- x[!weekdays(x) %in% c('Saturday','Sunday')]
    

    This answer is valid only with an English based system. For instance, in a French version, 'Saturday' and 'Sunday' must be translated into 'samedi' and 'dimanche'

    0 讨论(0)
提交回复
热议问题