【发布时间】:2022-01-10 13:19:10
【问题描述】:
我有一个从 .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 重新定义为日期时间列。我设法为日期和时间设置了正确的格式,除了一个细节:月份始终是缩写而不是数字。所以二月是二月而不是 02,十月是十月而不是 10,等等。
chron 函数中是否有需要更改的参数?是否还有其他函数不会为时间创建书面缩写?
感谢大家的帮助!
【问题讨论】:
-
如果你能创建一个可重现的例子那就太好了,例如通过
dput显示importeddataframe的示例。 -
@JKupzig 我已经包含了 dput(importeddataframe) 的返回,只有列 V1 和 V2 是感兴趣的。这有帮助吗?