How to extract the week number of the month in R?

Asked

Viewed 704 times

4

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 example of the date 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 the R to extract from hoje the week of the month?

1 answer

4


You can use the function lubridate::day together with the function ceiling:

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
  • 1

    It worked perfectly. I had confused the amount of weeks in the month but I’ve corrected the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.