简体   繁体   中英

Convert four digit year values to class Date

I've an integer column in my dataset which has four digit year values, like:

 c(2001, 2002, 2002, 2002, 2003, 2005) 

I try to convert the four digit year to class Date using as.Date :

year <- as.Date(as.character(data_file$evtYear), format = "%Y")

But the output is:

"2001-05-15" "2002-05-15" "2002-05-15" "2002-05-15" "2003-05-15" "2005-05-15"

This is giving the wrong output. It's giving two year values in one date (both 2001 and also 15).

I just want the convert my four digit year part from the original data to 'Year' as class Date . Expected out put is simply:

2001 2002 2002 2002 2003 2005 

But their class should be of Date type.

How to achieve this in R?

Based on the comments it turned out that the person asking the question did not need to change a numeric year to "Date" class; nevertheless, the question asked how to do it so here is an answer.

Here are a few ways to create a "Date" class object from a 4 digit numeric year. All use as.Date :

yrs <- c(2001, 2002, 2002, 2002, 2003, 2005)

1) ISOdate

as.Date(ISOdate(yrs, 1, 1))  # beginning of year
as.Date(ISOdate(yrs, 12, 31))  # end of year

This ISOdate solution is a bit tricky because it creates an intermediate POSIXct object so time zone problems could exist. You might prefer one of the following.

2) paste

as.Date(paste(yrs, 1, 1, sep = "-")) # beginning of year
as.Date(paste(yrs, 12, 31, sep = "-")) # end of year

3) zoo::as.yearmon

library(zoo)

as.Date(as.yearmon(yrs)) # beginning of year
as.Date(as.yearmon(yrs) + 11/12, frac = 1) # end of year

Note: If y is the result for any of the above then format(y, "%Y") gives the character year and as.numeric(format(y, "%Y")) gives the numeric year.

As already recognized by the OP, a year alone does not make up a valid date because month and day are not specified.

However, some date and date-time conversion functions, eg, ymd() , parse_date_time() , in the lubridate package recognize a parameter truncated to allow for parsing of incomplete dates:

yrs <- c(2001, 2002, 2002, 2002, 2003, 2005)
lubridate::ymd(yrs, truncated = 2L)
 [1] "2001-01-01" "2002-01-01" "2002-01-01" "2002-01-01" "2003-01-01" "2005-01-01"

The years have been completed by 1st of January to make a valid date. The result is of class Date .

You can do:

library(lubridate)
yrs <- c(2001, 2002, 2002, 2002, 2003, 2005)
yr <- as.Date(as.character(yrs), format = "%Y")
y <- year(yr)

Output:

2001 2002 2002 2002 2003 2005

A lubridate answer:

  library(lubridate)
  year <- ymd(sprintf("%d-01-01",data_file$evtYear))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM