# How to Get Week Number from Dates in R (With Examples)

You can use the following methods to get the week number from a date in R:

Method 1: Get Week Number Using Base R

```strftime(df\$date_column, format = '%V')
```

Method 2: Get Week Number Using Lubridate Package

```library(lubridate)

isoweek(ymd(df\$date_column))
```

The following examples show how to use each method with the following data frame in R:

```#create data frame
df <- data.frame(date=as.Date(c('1/8/2022', '1/9/2022', '2/10/2022', '2/15/2022',
'3/5/2022', '3/22/2022', '3/27/2022'), '%m/%d/%Y'),
sales=c(8, 14, 22, 23, 16, 17, 23))

#view data frame
df

date sales
1 2022-01-08     8
2 2022-01-09    14
3 2022-02-10    22
4 2022-02-15    23
5 2022-03-05    16
6 2022-03-22    17
7 2022-03-27    23```

## Example 1: Get Week Number Using Base R

The following code shows how to use the strftime() function to get the week number from the date column in the data frame:

```#add column to show week number
df\$week_num <- strftime(df\$date, format = "%V")

#view updated data frame
df

date sales week_num
1 2022-01-08     8       01
2 2022-01-09    14       01
3 2022-02-10    22       06
4 2022-02-15    23       07
5 2022-03-05    16       09
6 2022-03-22    17       12
7 2022-03-27    23       12```

The new column called week_num displays the week number for the value in the date column.

Note: From the documentation, here is how %V% calculates date numbers:  “the week number of the year (Monday as the first day of the week) as a decimal number [01,53]. If the week containing 1 January has four or more days in the new year, then it is considered week 1. Otherwise, it is the last week of the previous year, and the next week is week 1.”

## Example 2: Get Week Number Using Lubridate Package

The following code shows how to use the isoweek() function from the lubridate package to get the week number from the date column in the data frame:

```#add column to show week number
df\$week_num <- isoweek(ymd(df\$date))

#view updated data frame
df

date sales week_num
1 2022-01-08     8        1
2 2022-01-09    14        1
3 2022-02-10    22        6
4 2022-02-15    23        7
5 2022-03-05    16        9
6 2022-03-22    17       12
7 2022-03-27    23       12```

The new column called week_num displays the week number for the value in the date column.

Notice that the week numbers match the ones calculated using the strftime() function in the previous example.