Apply date format on a numeric

image
Hi, all ...

I have a numeric column DateNum in data4 dataframe. I would like to have it displayed in the form of year-month-date format while keeping the variable type as numeric.

Please guide how to achieve it?

You can change the numbers to dates and still recover the numeric values.

DF <- data.frame(DateNum = c(16151, 16157))
str(DF)
#> 'data.frame':    2 obs. of  1 variable:
#>  $ DateNum: num  16151 16157
DF$DateNum <- as.Date(DF$DateNum, origin = "1970-01-01")
str(DF)
#> 'data.frame':    2 obs. of  1 variable:
#>  $ DateNum: Date, format: "2014-03-22" "2014-03-28"
DF$DateNum
#> [1] "2014-03-22" "2014-03-28"
class(DF$DateNum)
#> [1] "Date"
as.numeric(DF$DateNum)
#> [1] 16151 16157

Created on 2023-04-14 with reprex v2.0.2

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.