【发布时间】:2020-03-18 04:36:11
【问题描述】:
我有以下数据框df:
timestamp ave.temperature min.temperature max.temperature
1/10/2017 0:00 27.20604 25.50000 28.54444
1/10/2017 1:00 27.24487 25.70000 28.60682
1/10/2017 2:00 27.26597 25.60667 28.76333
1/10/2017 3:00 27.16104 25.60000 28.32833
我想将timestamp 列转换为表单:
timestamp ave.temperature min.temperature max.temperature
1/10/2017 00:00:00 27.20604 25.50000 28.54444
1/10/2017 01:00:00 27.24487 25.70000 28.60682
1/10/2017 02:00:00 27.26597 25.60667 28.76333
1/10/2017 03:00:00 27.16104 25.60000 28.32833
到目前为止,我已经尝试过 df$timestamp <- as.Date(df$timestamp, format="%d/%m/%Y %H:%M:%S"),但它返回了 NA 列的 NA 值:
timestamp ave.temperature min.temperature max.temperature
NA 27.20604 25.50000 28.54444
NA 27.24487 25.70000 28.60682
NA 27.26597 25.60667 28.76333
NA 27.16104 25.60000 28.32833
但是,使用df$timestamp <- as.Date(df$timestamp, format="%d/%m/%Y %H:%M"),它变成:
timestamp ave.temperature min.temperature max.temperature
1/10/2017 27.20604 25.50000 28.54444
1/10/2017 27.24487 25.70000 28.60682
1/10/2017 27.26597 25.60667 28.76333
1/10/2017 27.16104 25.60000 28.32833
在这一点上,我真的不知道为什么会发生这种情况,并且到目前为止还没有设法在网上找到任何类似的案例.. 一些见解将不胜感激!
备注:使用as.POSIXct(df$timestamp, format="%d/%m/%Y %H:%M:%S") 也会导致NA 用于timestamp 列。
更新:找到了解决方案,但围绕着as.Date 的谜团仍然存在(请参阅下面的答案)。
【问题讨论】:
标签: r dataframe datetime timezone