R - 无法格式化日期列
R - fails to format a date column
我有这个数据框:
date NO2
2 01/11/16 00:00 56.8
3 01/11/16 00:15 52.3
4 01/11/16 00:30 49.6
5 01/11/16 00:45 55.5
6 01/11/16 01:00 48
7 01/11/16 01:15 45
我想覆盖日期格式,例如:
2016-01-11 00:00
这是我的代码:
# Convert chr to POSIXct, format for openair.
DF$date <- as.POSIXct(DF$date)
# Make sure that date values are POSIXct, format.
str(anonSpeciesDF)
'data.frame': 14497 obs. of 2 variables:
$ date: POSIXct, format: "1-11-16 00:00:00" ...
$ NO2 : Factor w/ 1093 levels "[\xb5g/m3]","10",..: 635 590 553 622 537 507 493 463 446 438 ...
# Format the date
DF$date <- format(DF$date, format='%Y-%m-%d %H:%M')
> View(DF)
结果:
date NO2
2 1-11-16 00:00 56.8
3 1-11-16 00:15 52.3
4 1-11-16 00:30 49.6
5 1-11-16 00:45 55.5
6 1-11-16 01:00 48
7 1-11-16 01:15 45
知道为什么吗?我做错了什么?
尝试向 as.POSIXct()
:
提供格式字符串
DF$date <- as.POSIXct(DF$date, format = "%d/%m/%y %H:%M")
我有这个数据框:
date NO2
2 01/11/16 00:00 56.8
3 01/11/16 00:15 52.3
4 01/11/16 00:30 49.6
5 01/11/16 00:45 55.5
6 01/11/16 01:00 48
7 01/11/16 01:15 45
我想覆盖日期格式,例如:
2016-01-11 00:00
这是我的代码:
# Convert chr to POSIXct, format for openair.
DF$date <- as.POSIXct(DF$date)
# Make sure that date values are POSIXct, format.
str(anonSpeciesDF)
'data.frame': 14497 obs. of 2 variables:
$ date: POSIXct, format: "1-11-16 00:00:00" ...
$ NO2 : Factor w/ 1093 levels "[\xb5g/m3]","10",..: 635 590 553 622 537 507 493 463 446 438 ...
# Format the date
DF$date <- format(DF$date, format='%Y-%m-%d %H:%M')
> View(DF)
结果:
date NO2
2 1-11-16 00:00 56.8
3 1-11-16 00:15 52.3
4 1-11-16 00:30 49.6
5 1-11-16 00:45 55.5
6 1-11-16 01:00 48
7 1-11-16 01:15 45
知道为什么吗?我做错了什么?
尝试向 as.POSIXct()
:
DF$date <- as.POSIXct(DF$date, format = "%d/%m/%y %H:%M")