as.Date()
st1 <- "2018-04-04"
as.Date(st1)
## [1] "2018-04-04"
f1 <- as.Date(c("2019-03-05", "2008-04-04"))
f1
## [1] "2019-03-05" "2008-04-04"
class(f1)
## [1] "Date"
f2 <- as.Date(c("2018/05/23","2017/11/30"))
f2
## [1] "2018-05-23" "2017-11-30"
f3 <- as.Date(32768, origin="1900-01-01") # excel
f3
## [1] "1989-09-19"
f4 <- as.Date(737488, origin="0000-01-01") # mathlab
f4
## [1] "2019-03-05"
f5 <- as.Date(0,origin="1970-01-01")
f5
## [1] "1970-01-01"
f3 > f4
## [1] FALSE
f3 != f4
## [1] TRUE
ISOdate(2019,3,5,tz="America/Bogota")
## [1] "2019-03-05 12:00:00 -05"
head(OlsonNames()) # nombres de las zonas horarias
## [1] "Africa/Abidjan" "Africa/Accra" "Africa/Addis_Ababa"
## [4] "Africa/Algiers" "Africa/Asmara" "Africa/Asmera"
isofecha <- ISOdate(2018,12,3,tz="America/Bogota")
class(isofecha)
## [1] "POSIXct" "POSIXt"
as.Date(isofecha)
## [1] "2018-12-03"
as.POSIXct(f1)
## [1] "2019-03-04 19:00:00 -05" "2008-04-03 19:00:00 -05"
dias <- f1[1] - f1[2]
dias
## Time difference of 3987 days
difftime(f1[1],f1[2],units="hours") # se puede escoger la unidad de tiempo
## Time difference of 95688 hours
Sys.Date() #fecha
## [1] "2019-03-25"
Sys.time() # fecha y hora actuales
## [1] "2019-03-25 06:29:27 -05"
Sys.time()-3600 # hace una hora
## [1] "2019-03-25 05:29:27 -05"
Formato para imprimir fechas
Formato para imprimir fechas
hoy <- Sys.Date() # hora actual
hoy # default año, mes, día
## [1] "2019-03-25"
format(hoy, format="%B %d %Y")
## [1] "March 25 2019"
format(hoy, format="%A %d %B %Y")
## [1] "Monday 25 March 2019"
format(hoy, format="%a %d %b %y")
## [1] "Mon 25 Mar 19"
round(Sys.time(), "hour")
## [1] "2019-03-25 06:00:00 -05"
trunc(Sys.time(), "day")
## [1] "2019-03-25 -05"
format(Sys.time(), "%Y")
## [1] "2019"
format(Sys.Date(),"%a")
## [1] "Mon"
weekdays
, months
y/o quarters
f5 <- as.Date(Sys.Date()+1)
f5
## [1] "2019-03-26"
weekdays(f5)
## [1] "Tuesday"
months(f5)
## [1] "March"
quarters(f5)
## [1] "Q1"
seq
:seq(Sys.Date(), length.out =5, by="1 day") # por día
## [1] "2019-03-25" "2019-03-26" "2019-03-27" "2019-03-28" "2019-03-29"
seq(Sys.Date(), length.out = 5, by="1 week") # por semana
## [1] "2019-03-25" "2019-04-01" "2019-04-08" "2019-04-15" "2019-04-22"
seq(Sys.Date(), length.out = 5, by="1 month") # por mes
## [1] "2019-03-25" "2019-04-25" "2019-05-25" "2019-06-25" "2019-07-25"
seq(Sys.Date(), length.out = 5, by="1 quarter") #por trimestre
## [1] "2019-03-25" "2019-06-25" "2019-09-25" "2019-12-25" "2020-03-25"
s1 <- seq(Sys.Date(), length.out = 5, by="1 year") # por año
min(s1)
## [1] "2019-03-25"
max(s1)
## [1] "2023-03-25"
mean(s1)
## [1] "2021-03-24"
Genere una serie diaria que empiece el 3 de Marzo de 2019 y termine el 10 de Mayo de 2019 sin sábados ni domingos.
suc <- seq(as.Date("2019-03-03"),as.Date("2019-05-10"),"day")
dias <- weekdays(seq(as.Date("2019-03-03"),as.Date("2019-05-10"),"day"))
suc[!(dias == "Sunday" | dias == "Saturday")]
## [1] "2019-03-04" "2019-03-05" "2019-03-06" "2019-03-07" "2019-03-08"
## [6] "2019-03-11" "2019-03-12" "2019-03-13" "2019-03-14" "2019-03-15"
## [11] "2019-03-18" "2019-03-19" "2019-03-20" "2019-03-21" "2019-03-22"
## [16] "2019-03-25" "2019-03-26" "2019-03-27" "2019-03-28" "2019-03-29"
## [21] "2019-04-01" "2019-04-02" "2019-04-03" "2019-04-04" "2019-04-05"
## [26] "2019-04-08" "2019-04-09" "2019-04-10" "2019-04-11" "2019-04-12"
## [31] "2019-04-15" "2019-04-16" "2019-04-17" "2019-04-18" "2019-04-19"
## [36] "2019-04-22" "2019-04-23" "2019-04-24" "2019-04-25" "2019-04-26"
## [41] "2019-04-29" "2019-04-30" "2019-05-01" "2019-05-02" "2019-05-03"
## [46] "2019-05-06" "2019-05-07" "2019-05-08" "2019-05-09" "2019-05-10"
fh = c("2002-06-09 12:45:40","2003-01-29 09:30:40",
"2002-09-04 16:45:40","2002-11-13 20:00:40",
"2002-07-07 17:30:40")
fh
## [1] "2002-06-09 12:45:40" "2003-01-29 09:30:40" "2002-09-04 16:45:40"
## [4] "2002-11-13 20:00:40" "2002-07-07 17:30:40"
library(knitr) # para usar `kable()`
set.seed(12345)
fe.aleat <- sample(seq(as.Date('2019/01/01'), as.Date('2019/12/31'), by="1 day"), 60)
hist(fe.aleat, "months", col="orchid",main="Fechas al azar de 2019",freq = TRUE,xlab="Fechas 2019", ylab="Frecuencia")
mean(fe.aleat);sd(fe.aleat)
## [1] "2019-06-24"
## [1] 105.584
kable(table(weekdays(fe.aleat)),format="html",caption = "Dias de la semana",col.names = c("Dia","Frecuencia"))
Dia | Frecuencia |
---|---|
Friday | 12 |
Monday | 8 |
Saturday | 5 |
Sunday | 8 |
Thursday | 10 |
Tuesday | 11 |
Wednesday | 6 |
z <- Sys.time()
z
## [1] "2019-03-25 06:29:27 -05"
class(z)
## [1] "POSIXct" "POSIXt"
unclass(z)
## [1] 1553513368
unclass(z)/86400
## [1] 17980.48
string <- "2018-11-30"
as.POSIXct(string)
## [1] "2018-11-30 -05"
as.POSIXlt(string)
## [1] "2018-11-30 -05"
unclass(as.POSIXct(string))
## [1] 1543554000
## attr(,"tzone")
## [1] ""
unclass(as.POSIXlt(string))
## $sec
## [1] 0
##
## $min
## [1] 0
##
## $hour
## [1] 0
##
## $mday
## [1] 30
##
## $mon
## [1] 10
##
## $year
## [1] 118
##
## $wday
## [1] 5
##
## $yday
## [1] 333
##
## $isdst
## [1] 0
##
## $zone
## [1] "-05"
##
## $gmtoff
## [1] NA
Lubridate
library(lubridate)
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
ymd(20190318)
## [1] "2019-03-18"
mdy(03202019)
## [1] "2019-03-20"
dmy(21032019)
## [1] "2019-03-21"
llega <- ymd_hms("20190101 12:12:12", tz = "America/Bogota")
llega
## [1] "2019-01-01 12:12:12 -05"
seva <- ymd_hms("20190301 15:15:15", tz = "America/Bogota")
seva
## [1] "2019-03-01 15:15:15 -05"
second(llega)
## [1] 12
wday(seva)
## [1] 6
interval(llega,seva)
## [1] 2019-01-01 12:12:12 -05--2019-03-01 15:15:15 -05
llega %--% seva
## [1] 2019-01-01 12:12:12 -05--2019-03-01 15:15:15 -05
Quiero llamar al señor Suzuki en Tokio el 19 de Marzo de 2019 a las 10:30 am, ¿Qué horas son en Bogotá?
cita <- ymd_hms("20190319 10:30:00", tz = "Asia/Tokyo")
cita
## [1] "2019-03-19 10:30:00 JST"
hora_Bog <- with_tz(cita, "America/Bogota")
hora_Bog
## [1] "2019-03-18 20:30:00 -05"
leap_year(2018)
## [1] FALSE
ymd(20200520) + dyears(1)
## [1] "2021-05-20"
ymd(20200520) + years(1)
## [1] "2021-05-20"
leap_year(2016)
## [1] TRUE
ymd(20160101) + dyears(1)
## [1] "2016-12-31"
ymd(20160101) + years(1)
## [1] "2017-01-01"