我試圖加載數據集到R和改變列的類型爲日期時間。把字符串到日期時間
strptime
,as.POSIXct
或as.Date
對於不同的情況應該可以工作;
這是我的代碼:
a <- structure(list(DATE = c("01/01/2011 12:00:00", "01/02/2011 12:00:00",
"01/03/2011 12:00:00", "01/04/2011 12:00:00", "01/05/2011 12:00:00",
"01/06/2011 12:00:00"), VAL = c(65.34447917, 65.23983333, 65.03183333,
64.89107292, 64.83333333, 64.848625), id = c("VT1-1", "VT1-1", "VT1-1",
"VT1-1", "VT1-1", "VT1-1")), .Names = c("DATE", "VAL", "id"), row.names = c(NA, -6L),
class = c("tbl_df", "tbl", "data.frame"))
b1 <- as.POSIXct(a$DATE, format = "%m/%d/%y %H:%M:%S")
b2 <- strptime(a$DATE,"%m/%d/%Y %H:%M/%S")
但他們只是返回NA
。這很可能是一個錯字;但在處理不同的日期時間格式時如何避免這種情況?
你有一個錯字 - 你用' 「%M /%d /%Y%H:%M /%S」',並在最後一個斜槓。你需要用冒號「%m /%d /%y%H:%M:%S」。但看到我的答案。 –