How to properly convert a POSIXct vector to a date vector

#1

I've not used vctrs before, but I was trying to convert a vector POSIXct dates to a vector of dates so I thought I'd give it a shot. I followed some of the documentation and I was able to hack my way to my desired result. However, I'm not sure I understand all the behaviors and I'm pretty confident I could have gotten to my desired result more easily/understandably. I provided my code and some commentary below. If anyone is able to take a look and provide some feedback I'd really appreciate it. Apologies in advance for any incoherence and thanks for your help!

library(timeDate)
library(vctrs)
library(magrittr)
library(purrr)
#> 
#> Attaching package: 'purrr'
#> The following object is masked from 'package:magrittr':
#> 
#>     set_names
# This will provide a vector of POSIXct values
dates <- attr(timeDate::holidayNYSE(),'Data') 
dates %>% str()
#>  POSIXct[1:9], format: "2019-01-01 05:00:00" "2019-01-21 05:00:00" ...
# When I change the class to date I get way in the future dates, I assume this is because it adds the time to 1970 origin instead of just days... 
class(dates) <- c('Date','Date')
str(dates)
#>  Date[1:9], format: "4235647-01-29" "4240378-03-08" "4247001-09-23" "4261185-03-07" ...
# Reset dates object.
dates <- attr(timeDate::holidayNYSE(),'Data') 
# When I run the dates into the vec_cast.POSIXct function it seems to stay in POSIXct format and shift the time back to my timezone.
vctrs::vec_cast.POSIXct(x=dates,to=date) %>% str()
#>  POSIXct[1:9], format: "2018-12-31 21:00:00" "2019-01-20 21:00:00" ...
# Apply as.Date to all the dates I see a list of dates. 
dates %>% 
  purrr::map(as.Date) %>% 
  str()
#> List of 9
#>  $ : Date[1:1], format: "2019-01-01"
#>  $ : Date[1:1], format: "2019-01-21"
#>  $ : Date[1:1], format: "2019-02-18"
#>  $ : Date[1:1], format: "2019-04-19"
#>  $ : Date[1:1], format: "2019-05-27"
#>  $ : Date[1:1], format: "2019-07-04"
#>  $ : Date[1:1], format: "2019-09-02"
#>  $ : Date[1:1], format: "2019-11-28"
#>  $ : Date[1:1], format: "2019-12-25"
# I'd rather work with a vector of dates so I apply unlist and the dates convert back to numbers.
dates %>% 
  purrr::map(as.Date) %>% 
  unlist() %>% 
  str()
#>  num [1:9] 17897 17917 17945 18005 18043 ...
# When I unlist and then vec_cast.Date to date I get my desired result.
dates  %>% 
  purrr::map(as.Date) %>%
  unlist() %>% 
  vctrs::vec_cast.Date(to=date) %>% str()
#>  Date[1:9], format: "2019-01-01" "2019-01-21" "2019-02-18" "2019-04-19" "2019-05-27" ...

Created on 2019-01-21 by the reprex package (v0.2.1)

0 Likes

#2

I don't get it, Why you don't simply use as.Date()?, It gives you the same result, a vector of class Date

library(timeDate)
dates <- attr(timeDate::holidayNYSE(),"Data")
as.Date(dates)
#> [1] "2019-01-01" "2019-01-21" "2019-02-18" "2019-04-19" "2019-05-27"
#> [6] "2019-07-04" "2019-09-02" "2019-11-28" "2019-12-25"

Created on 2019-01-21 by the reprex package (v0.2.1)

2 Likes

#3

@andresrcs I don't know where I went wrong, I thought I tried that first but must have screwed up in my code. Thanks for the response and sorry for the silly question!

0 Likes

#4

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

0 Likes

closed #5

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

0 Likes