Double to date conversion

Hello everyone! ,

I have to work on a data set and create a plot for an example of it that will provide monthly data with a count of orders. On data sets, a date column is created based on the double data type and class of date. I have to split the column based on months. I have to change the data type from double to date. I have tried with as.Date(), as.Date.POSIXct(), and as.Date.POSIXlt(). But I'm unable to change its data type. Could anyone please help me on this issue?

Regards,
Mohana Krishna Alla.

Hi, welcome!

We don't really have enough info to help you out. Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

Thanks for your suggestion andresrcs,

I have to change the type of column data from double to date format. I have tried using functions such as.Date(), as.Date.POSIXct(), and as.Date.POSIXlt(), but I'm unable to change its data type. Could anyone help me with this issue?

Regards,
Mohana Krishna Alla.

Please post some sample data in a copy/paste friendly format (as explained on the reprex guide I linked for you)

Please look over this code and output. Please read the question and understand my request.

date<-apps$RECVDATE
head(date)

output:
[1] "2022-01-01" "2022-01-01" "2022-01-01" "2022-01-01" "2022-01-01"
[6] "2022-01-01"

Try2:

date1<-as.Date(date)
typeof(date1)

output:
[1] "double"
Try3

date2<-as.Date.POSIXct(date)
typeof(date2)

output:
[1] "double"

I think you want to use class() instead of typeof() because the latter returns the R's internal storage type and dates are stored internally as the number (double) of days since an origin, so, there is no date internal type.

date <- c("2022-01-01", "2022-01-01", "2022-01-01",
          "2022-01-01", "2022-01-01", "2022-01-01")
date1 <- as.Date(date)

class(date1)
#> [1] "Date"

Created on 2022-11-26 with reprex v2.0.2