我有一個從 .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 函式中更改一個引數嗎?是否有另一個函式不會為時間創建書面縮寫?
謝謝大家的幫助!
uj5u.com熱心網友回復:
使用時間:
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"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/409564.html
標籤:
上一篇:列出理解以查找資料框是否是周末
下一篇:創建一系列季度
