R converting a factor YYYY-MM to a date

后端 未结 2 593
长发绾君心
长发绾君心 2021-01-16 07:48

I have a dataframe with a date in the form YYYY-MM, class factor and I am trying to convert it to class date.

I tried:

Date <- c(\"2015-08\",\"201         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-16 08:23

    R does not support Dates in the format "%Y-%m"... A day is needed

    You can do the following:

    as.POSIXct(paste0(as.character(df[,1]),"-01"), format = "%Y-%m-%d")
    

    Resulting in

    "2015-08-01 CEST" "2015-09-01 CEST" "2015-08-01 CEST"
    

提交回复
热议问题