计算星期日,星期一,......,星期六的数量

时间:2012-07-25 14:58:42

标签: r

我喜欢计算2001年的星期日,星期一,星期二,......,星期六的数量。请将以下日期{4月1日,4月5日,4月13日,12月25日和12月26日}作为公众假期并考虑他们是星期天。我怎么能在R? - 谢谢

2 个答案:

答案 0 :(得分:5)

以下是立陶宛语版本:

dates <- as.Date("2001-01-01") + 0:364
wd <- weekdays(dates)
idx <- which(dates %in% as.Date(c("2001-01-01", "2001-04-05", 
             "2001-04-13", "2001-12-25", "2001-12-26")))
wd[idx] <- "sekmadienis"
table(wd)
wd
   antradienis ketvirtadienis   penktadienis    pirmadienis    sekmadienis    šeštadienis   trečiadienis 
            51             51             51             52             57             52             51 

答案 1 :(得分:3)

尝试以下方法:

# get all the dates you need
dates <- seq(from=as.Date("2001-01-01"), to=as.Date("2001-12-31"), by="day")

# makes sure the dates are in POSIXlt format
dates <- strptime(dates, "%Y-%m-%d")

# get rid of the public holidays
pub <- strptime(c(as.Date("2001-01-01"), 
                 as.Date("2001-04-05"), 
                 as.Date("2001-04-13"), 
                 as.Date("2001-12-25"), 
                 as.Date("2001-12-26")), "%Y-%m-%d")
dates <- dates[which(!dates%in%pub)]


# To see the day of the week
weekdays <- dates$wday

# Now, count the number of Mondays for example:
length(which(weekdays == 1))

有关详细信息,请参阅the documentation for DateTimeClasses。记得在星期日加上5点。