
Sign up to save your podcasts
Or


In this video, we are extracting the year from a date variable. Sometimes there's just too much information in the data we have, like when we have a variable with the exact date but, we only want the year of an event.
A. If the date is stored as a string, like "20250911", we can use the substring function built into R.
B. If the date is stored as a date object, using a substring may fail. We can format the date so that only the year is shown.
R-code used:
my = data.frame(when = c("20230911", "20240911", "20250911", "20250110", "20230201", "20241225", "20250101"))
substr(my$when,1,4)
my$year = as.numeric(format(my$dates, "%Y"))
00:00 Introduction
By In this video, we are extracting the year from a date variable. Sometimes there's just too much information in the data we have, like when we have a variable with the exact date but, we only want the year of an event.
A. If the date is stored as a string, like "20250911", we can use the substring function built into R.
B. If the date is stored as a date object, using a substring may fail. We can format the date so that only the year is shown.
R-code used:
my = data.frame(when = c("20230911", "20240911", "20250911", "20250110", "20230201", "20241225", "20250101"))
substr(my$when,1,4)
my$year = as.numeric(format(my$dates, "%Y"))
00:00 Introduction