Mostrando entradas con la etiqueta strftime. Mostrar todas las entradas
Mostrando entradas con la etiqueta strftime. Mostrar todas las entradas

2019-06-02

How to get week numbers from dates in R?


Problem

We would like to calculate the week number of a given date in R.

c("2014-03-16", "2014-03-17","2014-03-18", "2014-01-01")
[1] "2014-03-16" "2014-03-17" "2014-03-18" "2014-01-01"

Solution

Using the function strftime passing the argument %V to obtain the week of the year as decimal number (01–53) as defined in ISO 8601. (More details in the documentarion: ?strftime)

strftime(c("2014-03-16", "2014-03-17","2014-03-18", "2014-01-01"), format = "%V")
[1] "11" "12" "12" "01"

Related entries

References

2019-05-07

Plot a continuous series with ggplot2

Problem

When we try to plot a continuous series, in our example WS (Winter Solstice), ggplot2 connects the last winter data point in March to the first winter data point in December.

  • Data
library(ggplot2)

getSeason <- function(DATES) {
#found here https://stackoverflow.com/questions/9500114/find-which-season-a-particular-date-belongs-to
WS <- as.Date("2012-12-15", format = "%Y-%m-%d") # Winter Solstice
SE <- as.Date("2012-3-15",  format = "%Y-%m-%d") # Spring Equinox
SS <- as.Date("2012-6-15",  format = "%Y-%m-%d") # Summer Solstice
FE <- as.Date("2012-9-15",  format = "%Y-%m-%d") # Fall Equinox

# Convert dates from any year to 2012 dates
d <- as.Date(strftime(DATES, format="2012-%m-%d"))

ifelse (d >= WS | d < SE, "Winter",
  ifelse (d >= SE & d < SS, "Spring",
    ifelse (d >= SS & d < FE, "Summer", "Fall")))
}

zz <- sample(1:10000,365)/1000
dag <- seq(as.Date("2014-01-01"), as.Date("2014-12-31"), by = "day")
seas <-  getSeason(dag)
test <- data.frame(zz,dag,seas)

ggplot(data=test, aes(x=dag,ymax=zz,ymin=0,fill=seas))+
geom_ribbon()

Solution

We can solve it by subsetting our data in two, above and below WS, and plotting two layers with geom_ribbon. Thus you convert the continuous WS series into two discrete sections.

library(dplyr)
ggplot() +
  geom_ribbon(data = filter(test, dag >= "2014-12-15") ,
              aes(x = dag, ymax = zz, ymin = 0, fill = seas)) +
  geom_ribbon(data = filter(test, dag < "2014-12-15") ,
              aes(x = dag, ymax = zz, ymin = 0, fill = seas))

Results

References

2018-02-04

Representar serie continua con geom_ribbon de ggplot2

Problema

Cuando intentamos representar una serie continua, en nuestro ejemplo WS (Winter Solstice), ggplot2 une el último dato de WS (invierno) en marzo con el primero en diciembre, obteniendo el siguiente resultado indeseado.

  • Datos
library(ggplot2)

getSeason <- function(DATES) {
#found here https://stackoverflow.com/questions/9500114/find-which-season-a-particular-date-belongs-to
WS <- as.Date("2012-12-15", format = "%Y-%m-%d") # Winter Solstice
SE <- as.Date("2012-3-15",  format = "%Y-%m-%d") # Spring Equinox
SS <- as.Date("2012-6-15",  format = "%Y-%m-%d") # Summer Solstice
FE <- as.Date("2012-9-15",  format = "%Y-%m-%d") # Fall Equinox

# Convert dates from any year to 2012 dates
d <- as.Date(strftime(DATES, format="2012-%m-%d"))

ifelse (d >= WS | d < SE, "Winter",
  ifelse (d >= SE & d < SS, "Spring",
    ifelse (d >= SS & d < FE, "Summer", "Fall")))
}

zz <- sample(1:10000,365)/1000
dag <- seq(as.Date("2014-01-01"), as.Date("2014-12-31"), by = "day")
seas <-  getSeason(dag)
test <- data.frame(zz,dag,seas)

ggplot(data=test, aes(x=dag,ymax=zz,ymin=0,fill=seas))+
geom_ribbon()

Solución

Dividimos los datos con dplyr en dos subconjuntos, igual o mayor que el 2014-12-15 y por debajo de dicha fecha. Así convertimos la serie WS en dos secciones discretas, evitando que una los dos extremos de WS.

library(dplyr)
ggplot() +
  geom_ribbon(data = filter(test, dag >= "2014-12-15") ,
              aes(x = dag, ymax = zz, ymin = 0, fill = seas)) +
  geom_ribbon(data = filter(test, dag < "2014-12-15") ,
              aes(x = dag, ymax = zz, ymin = 0, fill = seas))

Resultados

Referencias

2017-06-27

Cómo extraer el número de la semana de una fecha en R


Problema

Queremos extraer el número de la semana de un vector de fechas en R.

c("2014-03-16", "2014-03-17","2014-03-18", "2014-01-01")
[1] "2014-03-16" "2014-03-17" "2014-03-18" "2014-01-01"

Solución

Empleamos la función strftime con el argumento %V para obtener el número de la semana (01–53) definido según la norma ISO 8601. Más información en la documentación: ?strftime

strftime(c("2014-03-16", "2014-03-17","2014-03-18", "2014-01-01"), format = "%V")
[1] "11" "12" "12" "01"

Entradas relacionadas

Referencias

Nube de datos