'Extracting year from a column that contains dates in m/d/yyyy format
I have a dataset called verts
imported from Excel with a column called Date
that contains dates in this format: m/d/yyyy
. I'm trying to extract the year from this column.
Here's what I'm currently trying:
verts$Date_converted <- strptime(verts$Date, format = "%m-%d-%y")
verts$Year <- format(verts$Date_converted, "%y")
This produced two new columns in R, as you can see in the image, but both columns only contain NA
.
Any ideas on what I'm doing wrong?
Solution 1:[1]
I recommend using package lubridate
for dates - it makes everything so much easier.
mdy()
quickly parses a date that's written in the US format - regardless of delimiter type.year()
extracts the year from an object in date format.
verts <- data.frame(Date = c("6/8/2004", "6/14/2004", "7/7/2006"))
verts <- verts |>
dplyr::mutate(Date_converted = lubridate::mdy(Date),
year = lubridate::year(Date_converted))
verts
#> Date Date_converted year
#> 1 6/8/2004 2004-06-08 2004
#> 2 6/14/2004 2004-06-14 2004
#> 3 7/7/2006 2006-07-07 2006
Created on 2022-04-22 by the reprex package (v2.0.1)
PS please don't use screenshots for data - we can't import that into R. Read here to make it more likely for you to get help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Andrea M |