Question:
I'm having trouble extracting the week number of the month from a specific date.
Example:
hoje = Sys.Date()
print(hoje)
[1] "2019-02-11"
In the date example above, hoje
would be the 2nd week of the month.
I know that:
wday() #extrai o dia da semana.
mday() #extrai o dia do mês.
month() #extrai o mês.
But how can I use R
to extract from hoje
the week of the month?
Answer:
You can use the lubridate::day
function in conjunction with the ceiling
function:
library(lubridate)
hoje<-Sys.Date() # Sys.Date retorna a data de hoje
[1] "2019-02-11"
day(hoje) # day retorna o dia do mês
[1] 11
day(hoje)/7 # dividir o dia pelo número de dias de uma semana (7)
[1] 1.571429
ceiling(day(hoje)/7) # retorna a semana do mês a qual a data está
[1] 2