Converting Integers to Date in R

前端 未结 2 428
面向向阳花
面向向阳花 2021-01-16 05:24

I want to convert my integer column to Date. the integer number looks like this (20160101). When using the as.Date function and by typing the origin argument to be \"2016-01

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

    just for your interest an alternative approach using lubridate and just keeping month and day.

    tmp <- as.integer(x = 20160101) tmp %>% lubridate::ymd() %>% format("%m-%d")

    Please notice, that you will be left with just a string afterwards.

    tmp %>% lubridate::ymd() %>% format("%m-%d") %>% is.Date()
    

    Is going to be FALSE. Without the cutoff of the year it will be TRUE. So to keep the date format, like Tim mentioned, an alternative lubridate approach would be:

     tmp %>% lubridate::ymd()
    

提交回复
热议问题