Incorrect Conversion of Date as a Factor to a Date

前端 未结 2 2017
春和景丽
春和景丽 2021-01-06 17:53

I am having trouble calculating a date that is imported in from a .csv file. What I want to do is take that date in the factor DateClosed and generate a date in a date fie

2条回答
  •  太阳男子
    2021-01-06 18:54

    DateClosed <- factor(c("7/30/2007","12/12/2007", "5/8/2009"))
    as.Date(DateClosed, format="%m/%d/%Y")
    

    Produces:

    [1] "2007-07-30" "2007-12-12" "2009-05-08"
    

    Notice the capital "Y" in the format param. The lower case "y" is for 2 digit years, so as.Date reads the first two digits of the year token ("20"), and then assumes that refers to just the last two digits of the year, and adds the current date's century (also "20"), so you end up with dates in 2020.

提交回复
热议问题