How to Parse Year + Week Number in R?
This is kinda like another question you may have seen before. :)
The key issue is: what day should a week number specify? Is it the first day of the week? The last? That's ambiguous. I don't know if week one is the first day of the year or the 7th day of the year, or possibly the first Sunday or Monday of the year (which is a frequent interpretation). (And it's worse than that: these generally appear to be 0-indexed, rather than 1-indexed.) So, an enumerated day of the week needs to be specified.
For instance, try this:
as.POSIXlt("2008 42 1", format = "%Y %U %u")
The %u
indicator specifies the day of the week.
Additional note: See ?strptime
for the various options for format conversion. It's important to be careful about the enumeration of weeks, as these can be split across the end of the year, and day 1
is ambiguous: is it specified based on a Sunday or Monday, or from the first day of the year? This should all be specified and tested on the different systems where the R code will run. I'm not certain that Windows and POSIX systems sing the same tune on some of these conversions, hence I'd test and test again.
How to get week number and year from date in R
Try this. (Two steps.)
x <- as.Date("1/1/2016", "%d/%m/%Y")
format(x, "%G")
[1] "2015"
format(x, "%V")
[1] "53"
See ?strptime
for details.
Convert Week number to date in data frame
You can do this in base R using as.Date
itself.
Based on your attempt it seems your locale is English, so you can try :
as.Date(paste(df$Week, df$Year, 'Sun'), '%U %Y %a')
#[1] "2019-01-06" "2019-01-13" "2020-01-05" "2020-01-05"
Convert week number to date
as.Date
is calling the 1 to 9 as NA as it is expects two digits for the week number and can't properly parse it.
To fix it, add in some - to split things up:
as.Date(paste(2014, df$Week, 1, sep="-"), "%Y-%U-%u")
how to determine month from year and week number?
We can paste
Week day , week number and year together to convert it into Date and then use format
to get the month name. Read ?strptime
for formatting options
format(as.Date(paste(1, df$Week, df$Year), "%u %U %Y"), "%B")
#[1] "December" "May" "November"
data
df <- structure(list(Week = c(52L, 20L, 47L), Year = 2018:2016),
class = "data.frame", row.names = c(NA, -3L))
Start a week from given day and get week number in R
We may specify the week_start
in floor_date
(week_start
- 1
would be Monday
and 7
would be Sunday
)
f1 <- function(dates, wk_start = 1) {
new <- lubridate::floor_date(dates, 'week', week_start = wk_start)
match(new, unique(new))
}
-checking
> library(dplyr)
> df %>%
+ mutate(week = f1(date, wk_start = 1)) %>%
+ head
date week
1 2022-01-01 1
2 2022-01-02 1
3 2022-01-03 2
4 2022-01-04 2
5 2022-01-05 2
6 2022-01-06 2
> df %>%
+ mutate(week = f1(date, wk_start = 2)) %>%
+ head
date week
1 2022-01-01 1
2 2022-01-02 1
3 2022-01-03 1
4 2022-01-04 2
5 2022-01-05 2
6 2022-01-06 2
> df %>%
+ mutate(week = f1(date, wk_start = 3)) %>%
+ head
date week
1 2022-01-01 1
2 2022-01-02 1
3 2022-01-03 1
4 2022-01-04 1
5 2022-01-05 2
6 2022-01-06 2
How to extract year and week number using base R?
This following works with base R commands:
year <- format(as.Date(df$created_at)+2, "%Y")
week <- format(as.Date(df$created_at)+2, "%U")
paste(year,week,sep = "-")
Note: You have to add 2 days, because the 1st January 2016 was a Friday and this would give you a week of 0. In format()
the week starts with Sunday.
Regards,
J_F
Related Topics
Directly Creating Dummy Variable Set in a Sparse Matrix in R
Set Certain Values to Na with Dplyr
Using Substitute to Get Argument Name
R Shiny Rest API Communication
Speed Up Plot() Function for Large Dataset
Setting Absolute Size of Facets in Ggplot2
How to Define the "Mid" Range in Scale_Fill_Gradient2()
Can Dcast Be Used Without an Aggregate Function
Automatically Create Formulas for All Possible Linear Models
How Convert Decimal to Posix Time
R - How to Get Row & Column Subscripts of Matched Elements from a Distance Matrix
Select Columns Based on String Match - Dplyr::Select
Multiple Graphs in One Canvas Using Ggplot2
Stacked Bar Chart in R (Ggplot2) with Y Axis and Bars as Percentage of Counts
Using Stargazer with Rstudio and Knitr
How to Force Specific Order of the Variables on the X Axis