0

I'm using this dataset (this is the head of my df) c("31 Mar 1999", "25 Mar 2019", "27 Nov 1996", "21 Jan 2003","22 Nov 2000", "11 Dec 2011")

I'm attempting to extract the year from these dates using this code:

netflix_and_disney$release_year <- as_date(netflix_and_disney$release_year)

I've tried many other date functions. However, I continue to get the same or similar error message relating to the non unambiguous format: "All formats failed to parse. No formats found."

How would I change the format in order to extract the year from each observation?

zx8754
  • 52,746
  • 12
  • 114
  • 209
yupper
  • 65
  • 6

1 Answers1

-2

Using lubridate :

x <- c("31 Mar 1999","25 Mar 2019","27 Nov 1996","21 Jan 2003","22 Nov 2000","11 Dec 2011")
library(lubridate)
year(dmy(x))
#[1] 1999 2019 1996 2003 2000 2011

In base R, assuming your locale is English

format(as.Date(x, "%d %b %Y"), "%Y")
#[1] "1999" "2019" "1996" "2003" "2000" "2011"
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213