Reputation: 355
I have a string that is yyyymmdd
and want to turn it into a date object in using sparklyr
so that I can subtract one date from the other.
This code works for yyyy-mm-dd hr:min:sec
temp_table <- taxi %>%
mutate(hrs = (unix_timestamp(tpep_dropoff_datetime) - unix_timestamp(tpep_pickup_datetime))
What cide would I use if instead of it being a string if yyyy-mm-dd hr:min:sec
it was a string of just yyyymmdd
?
I tried something like this but does not work.
temp_table <- taxi %>%
mutate(hrs = (datetime.strptime(tpep_dropoff_datetime) - datetime.strptime(tpep_pickup_datetime))
Upvotes: 1
Views: 2414
Reputation: 136
Try this:
temp_table <- taxi %>%
mutate(hrs = (unix_timestamp(tpep_dropoff_datetime,'yyyyMMdd') - unix_timestamp(tpep_pickup_datetime,'yyyyMMdd'))
Upvotes: 0
Reputation: 5018
Use as.Date
and specify the format
as.Date("20150102", format = "%Y%m%d")
## [1] "2015-01-02"
Codes that you can use in specifying the format can be found by running ?strptime
Upvotes: 4