0

I'm trying to convert my year variable into a Date class

     year     type       sum
1   99 NON-ROAD  522.9400
2   99 NONPOINT 2107.6250
3   99  ON-ROAD  346.8200
4   99    POINT  296.7950
5   02 NON-ROAD  240.8469
6   02 NONPOINT 1509.5000

The class of year is chr.

Each time I use as.Date(df$year, "%y") it returns

[1] "1999-12-23" "1999-12-23" "1999-12-23" "1999-12-23" "2002-12-23" "2002-12-23".....

I only want to change the class, not the format, retaining the 2-digit YR formatting.

How can I change the class, but maintain the formatting, of the year variable?

user3200293
  • 181
  • 5
  • 18
  • 1
    A date consists of a year, a month and a day. If you don't have this information it's not a date. – Roland Dec 23 '15 at 14:13
  • Is there anyway of making it the start of the year rather than the end? – user3200293 Dec 23 '15 at 15:58
  • You can convert your `year` column to January 1st dates as follows: `df$year2 = as.Date(paste0(df$year, "-01-01"), format="%y-%m-%d")`. But it will still display the full date. Using `format="%y"` won't display as the two-digit year. Rather, R will take those two digits and build a full date around them using today's date for the day and month. Maybe if you give us more context about what you're trying to do, we can come up with an approach that gets you where you want to go. – eipi10 Dec 23 '15 at 17:36

0 Answers0