如何在 R 中将日期格式更改为 2019-Feb-07 到 2019-02-07?
How to change date format to 2019-Feb-07 to 2019-02-07 in R?
我有一个从 .txt 文件中读取数据的代码,其中有日期列 dd/mm/yyyy 和时间列 HH:MM:SS,我想将这些列合并为一个日期时间列 yyyy/mm/dd HH:MM:SS.
数据帧结构为:
structure(list(V1 = c("05/02/2019", "07/02/2019", "08/02/2019",
"09/02/2019", "10/02/2019", "11/02/2019", "12/02/2019", "13/02/2019"
),
V2 = c("10:37:34", "00:00:00", "00:00:00", "00:00:00", "00:00:00",
"00:00:00", "00:00:00", "00:00:00"),
V3 = c("0,2", "0", "0",
"0", "0", "0", "0", "0"),
V4 = c("NEW-BLOCK", "NEW-DAY", "NEW-DAY",
"NEW-DAY", "NEW-DAY", "NEW-DAY", "NEW-DAY", "NEW-DAY"),
V5 = c("",
"", "", "", "", "", "", ""),
V6 = c("", "", "", "", "", "", "",
"")), row.names = 5:12, class = "data.frame")
我的工作代码是:
importeddataframe <-read.table(k,fill =TRUE, stringsAsFactors =FALSE )
importeddataframe$V1<-chron(importeddataframe$V1, importeddataframe$V2, format =c(dates="d/m/y", times="h:m:s"))
importeddataframe$V1<-chron(importeddataframe$V1, importeddataframe$V2, format =c(dates="yyyy-mm-dd", times="H:M:S"))
其中 k 是路径源,V1 是日期列,V2 是时间列,我正在尝试使用 chron 将 V1 重新定义为日期时间列。除了一个细节之外,我设法为日期和时间设置了正确的格式:月份始终是缩写而不是数字。所以二月将是 Feb 而不是 02,October 将是 Oct 而不是 10,等等
我需要在 chron 函数中更改参数吗?有没有其他函数不创建时间的书面缩写?
感谢大家的帮助!
使用计时:
importeddataframe$V1<-chron::chron(importeddataframe$V1, importeddataframe$V2, format =c(dates="d/m/y", times="h:m:s"), out.format=c(dates="y-m-d", times="H:M:S"))
> importeddataframe$V1
[1] (19-02-05 10:37:34) (19-02-07 00:00:00) (19-02-08 00:00:00) (19-02-09 00:00:00) (19-02-10 00:00:00) (19-02-11 00:00:00) (19-02-12 00:00:00)
[8] (19-02-13 00:00:00)
使用基数 R
importeddataframe$Time <- as.POSIXct(paste(importeddataframe$V1,importeddataframe$V2), format="%d/%m/%Y %H:%M:%S" )
> importeddataframe$Time
[1] "2019-02-05 10:37:34 CET" "2019-02-07 00:00:00 CET" "2019-02-08 00:00:00 CET" "2019-02-09 00:00:00 CET" "2019-02-10 00:00:00 CET"
[6] "2019-02-11 00:00:00 CET" "2019-02-12 00:00:00 CET" "2019-02-13 00:00:00 CET"
我有一个从 .txt 文件中读取数据的代码,其中有日期列 dd/mm/yyyy 和时间列 HH:MM:SS,我想将这些列合并为一个日期时间列 yyyy/mm/dd HH:MM:SS.
数据帧结构为:
structure(list(V1 = c("05/02/2019", "07/02/2019", "08/02/2019",
"09/02/2019", "10/02/2019", "11/02/2019", "12/02/2019", "13/02/2019"
),
V2 = c("10:37:34", "00:00:00", "00:00:00", "00:00:00", "00:00:00",
"00:00:00", "00:00:00", "00:00:00"),
V3 = c("0,2", "0", "0",
"0", "0", "0", "0", "0"),
V4 = c("NEW-BLOCK", "NEW-DAY", "NEW-DAY",
"NEW-DAY", "NEW-DAY", "NEW-DAY", "NEW-DAY", "NEW-DAY"),
V5 = c("",
"", "", "", "", "", "", ""),
V6 = c("", "", "", "", "", "", "",
"")), row.names = 5:12, class = "data.frame")
我的工作代码是:
importeddataframe <-read.table(k,fill =TRUE, stringsAsFactors =FALSE )
importeddataframe$V1<-chron(importeddataframe$V1, importeddataframe$V2, format =c(dates="d/m/y", times="h:m:s"))
importeddataframe$V1<-chron(importeddataframe$V1, importeddataframe$V2, format =c(dates="yyyy-mm-dd", times="H:M:S"))
其中 k 是路径源,V1 是日期列,V2 是时间列,我正在尝试使用 chron 将 V1 重新定义为日期时间列。除了一个细节之外,我设法为日期和时间设置了正确的格式:月份始终是缩写而不是数字。所以二月将是 Feb 而不是 02,October 将是 Oct 而不是 10,等等
我需要在 chron 函数中更改参数吗?有没有其他函数不创建时间的书面缩写?
感谢大家的帮助!
使用计时:
importeddataframe$V1<-chron::chron(importeddataframe$V1, importeddataframe$V2, format =c(dates="d/m/y", times="h:m:s"), out.format=c(dates="y-m-d", times="H:M:S"))
> importeddataframe$V1
[1] (19-02-05 10:37:34) (19-02-07 00:00:00) (19-02-08 00:00:00) (19-02-09 00:00:00) (19-02-10 00:00:00) (19-02-11 00:00:00) (19-02-12 00:00:00)
[8] (19-02-13 00:00:00)
使用基数 R
importeddataframe$Time <- as.POSIXct(paste(importeddataframe$V1,importeddataframe$V2), format="%d/%m/%Y %H:%M:%S" )
> importeddataframe$Time
[1] "2019-02-05 10:37:34 CET" "2019-02-07 00:00:00 CET" "2019-02-08 00:00:00 CET" "2019-02-09 00:00:00 CET" "2019-02-10 00:00:00 CET"
[6] "2019-02-11 00:00:00 CET" "2019-02-12 00:00:00 CET" "2019-02-13 00:00:00 CET"