Dev P
Dev P

Reputation: 449

Can we extract week numbers from the date

I got a dataframe df. Is there a way to populate the week numbers (as shown the second column in the dataframe). I mean the week numbers should be continuous. For example.

df <- structure(list(Date = structure(c(1527120000, 1527206400, 1527292800, 
1527379200, 1527465600, 1527552000, 1527638400, 1527724800, 1527811200, 
1527897600, 1527984000, 1528070400, 1528156800, 1528243200, 1528329600, 
1528416000, 1528502400, 1528588800, 1528675200, 1528761600, 1528848000, 
1528934400, 1529020800, 1529107200, 1529193600, 1529280000, 1529366400, 
1529452800, 1529539200, 1529625600, 1529712000, 1529798400, 1529884800, 
1529971200, 1530057600, 1530144000), class = c("POSIXct", "POSIXt"
), tzone = "UTC"), Week = c(1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 
2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 
5, 5, 5, 6)), row.names = c(NA, -36L), class = c("tbl_df", "tbl", 
"data.frame"))

Here the first date is 24-05-2018 and hence it should be numbered as 1. The number should be continued for the next 6 days and then 2 should start for the next 7 days. After 4 week, the number should not be again 1, the number should be numbered as 5 and so on. So basically I need to check the mean of the list of time series for the span of 4 years. So right from day 1(say in 2000) to last day (say in 2014), the week number should be populated. Is there a way?

Upvotes: 3

Views: 86

Answers (4)

akrun
akrun

Reputation: 887981

We can use gl

library(dplyr)
df %>% 
   mutate(Week = as.integer(gl(n(), 7, n())))

Upvotes: 2

tmfmnk
tmfmnk

Reputation: 40181

You can also try:

df %>%
 mutate(Week = ceiling(row_number()/7))

   Date                 Week
   <dttm>              <dbl>
 1 2018-05-24 00:00:00     1
 2 2018-05-25 00:00:00     1
 3 2018-05-26 00:00:00     1
 4 2018-05-27 00:00:00     1
 5 2018-05-28 00:00:00     1
 6 2018-05-29 00:00:00     1
 7 2018-05-30 00:00:00     1
 8 2018-05-31 00:00:00     2
 9 2018-06-01 00:00:00     2
10 2018-06-02 00:00:00     2

It does not take into account the dates, it just groups together every 7 rows.

The same with base R:

ceiling(1:NROW(df)/7)

 [1] 1 1 1 1 1 1 1 2 2 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 6

Upvotes: 1

slava-kohut
slava-kohut

Reputation: 4233

You can use lubridate:

library(lubridate)
df$Data <- trunc((ymd(df$Date) - min(ymd(df$Date)))/dweeks(1)) + 1

1 is needed to start numbering of weeks from 1. trunc gets rid of decimal places.

Upvotes: 2

MrFlick
MrFlick

Reputation: 206606

Date values are basically the number of seconds since a particular date. Here we can calculate the week offset with a bit of modulo math

df %>% 
  mutate(NewWeek = as.numeric(Date-min(Date)) %/% (60*60*24*7) + 1)

We subtract off the first date, then see how many seven day periods have passed.

Upvotes: 2

Related Questions