2011-09-29 42 views

回答

4

您可以使用seq命令。例如,

##Specify you want 10 dates starting on 1990-01-01 
R> seq(as.Date("1990-01-01"), length.out=10, by="1 day") 
[1] "1990-01-01" "1990-01-02" "1990-01-03" "1990-01-04" "1990-01-05" 
[6] "1990-01-06" "1990-01-07" "1990-01-08" "1990-01-09" "1990-01-10" 

##Specify the start and end with increment 
R> seq(as.Date("1990-01-01"), as.Date("1990-01-10"), by="1 day") 
[1] "1990-01-01" "1990-01-02" "1990-01-03" "1990-01-04" "1990-01-05" 
[6] "1990-01-06" "1990-01-07" "1990-01-08" "1990-01-09" "1990-01-10" 

只得到個工作日,您可以使用chron庫:

days = seq(as.Date("1990-01-01"), as.Date("1990-12-31"), by="1 day") 
library(chron) 
weekDays = days[!is.weekend(days)] 
+0

感謝您的回答,但我不想週末一天... – Eva

14

@csgillespie:chron提供的功能is.weekend

days = seq(as.Date("1990-01-01"), as.Date("1990-12-31"), by="1 day") 
library(chron) 
weekDays = days[!is.weekend(days)] 

## let's check the result with the function weekdays 
weekdays(weekDays) 

此外,你可以得到相同的結果,而無需使用formatchron

isWeekend <- function(x) {format(x, '%w') %in% c(0, 6)} 
weekDays2 = days[!isWeekend(days)] 
1

有一個叫?weekdaysbase功能。

startDate = "1990-01-01" 
endDate = "1990-12-31" 

x <- seq(as.Date(startDate), to = as.Date(endDate), by="1 day") 

x[!weekdays(x) %in% c("Sunday", "Saturday")] 

但是,由於日期的實際名稱將是區域設置特定的,因此請務必正確設置它們。

請注意,weekdays只是format(x, "%A")的包裝。有關格式代碼的詳細信息,請參閱?strptime

相關問題