I have a date that I convert to a numeric value and want to convert back to a date afterwards.
Converting date to numeric:
date1 = as.POSIXct('2017-12-30 15:00:00')
date1_num = as.numeric(date1)
# 1514646000
Run Code Online (Sandbox Code Playgroud)
Reconverting numeric to date:
as.Date(date1_num, origin = '1/1/1970')
# "4146960-12-12"
Run Code Online (Sandbox Code Playgroud)
What am I missing with the reconversion? I'd expect the last command to return my original date1.
由于数值向量是从具有时间分量的对象创建的,因此也可以采用相同的方式进行重新转换,即首先POSIXct转换为,然后用as.Date
as.Date(as.POSIXct(date1_num, origin = '1970-01-01'))
#[1] "2017-12-30"
Run Code Online (Sandbox Code Playgroud)