voglio generare una serie storica con tutte le date di business nel range:generando una serie di tempo con uno specifico inizio e di fine
startDate = "1990-01-01"
endDate = "1990-12-31"
Per esempio "1990-01-01", "1990-1901 -02 ", ...
voglio generare una serie storica con tutte le date di business nel range:generando una serie di tempo con uno specifico inizio e di fine
startDate = "1990-01-01"
endDate = "1990-12-31"
Per esempio "1990-01-01", "1990-1901 -02 ", ...
È 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)]
@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)]
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.
grazie per la tua risposta ma non voglio giorno del fine settimana ... – Eva