2011-09-29 6 views

risposta

4

È possibile utilizzare semplicemente il comando seq. Ad esempio,

##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" 

o

##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" 

Per ottenere solo giorni lavorativi, è possibile utilizzare la libreria chron:

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

grazie per la tua risposta ma non voglio giorno del fine settimana ... – Eva

14

@csgillespie: chron fornisce la funzione 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) 

Inoltre, è possibile ottenere gli stessi risultati senza chron utilizzare format:

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

C'è una funzione chiamata base?weekdays.

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")] 

Ma, dal momento che i nomi reali dei giorni saranno locale specifico, accertarsi di impostare correttamente quelli.

Nota che weekdays è solo un wrapper su format(x, "%A"). Vedi ?strptime per i dettagli sui codici di formato.

Problemi correlati