无法转换为R中的日期时间
当我尝试将Datetime转换为POSIXct时,如下所示:无法转换为R中的日期时间,r,posixct,R,Posixct,当我尝试将Datetime转换为POSIXct时,如下所示: structure(list(Datetime = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "2016-10-19 00:00:00", class = "factor")), .Names = "Datetime", row.names = c(NA, -6L), class = "data.frame") 为什么我丢失了数据中的小时、分、秒?你没有丢失它,它们只是没有显示出来
structure(list(Datetime = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "2016-10-19 00:00:00", class = "factor")), .Names = "Datetime", row.names = c(NA,
-6L), class = "data.frame")
为什么我丢失了数据中的小时、分、秒?你没有丢失它,它们只是没有显示出来(因为00:00:00意味着没有重要的时间)。尝试设置小时、分钟和秒的值,它们将显示出来
Datetime
1 2016-10-19
2 2016-10-19
3 2016-10-19
4 2016-10-19
5 2016-10-19
6 2016-10-19
dput(t)
structure(list(Datetime = structure(c(1476849600, 1476849600,
1476849600, 1476849600, 1476849600, 1476849600), class = c("POSIXct",
"POSIXt"), tzone = "")), .Names = "Datetime", row.names = c(NA,
-6L), class = "data.frame")
t我想马修·伦德伯格以前回答过这个问题。您没有丢失它,只是没有显示。@aichao,我想我丢失了值。根据文档(请参见?DateTimeClasses
):Class“POSIXct”表示自1970年初以来(UTC时区)的(有符号)秒数,作为数字向量。这些是上一个dput()
中相当大的整数值所代表的内容。
Datetime
1 2016-10-19
2 2016-10-19
3 2016-10-19
4 2016-10-19
5 2016-10-19
6 2016-10-19
dput(t)
structure(list(Datetime = structure(c(1476849600, 1476849600,
1476849600, 1476849600, 1476849600, 1476849600), class = c("POSIXct",
"POSIXt"), tzone = "")), .Names = "Datetime", row.names = c(NA,
-6L), class = "data.frame")
t <- structure(list(
Datetime = structure(c(1L, 1L, 1L, 1L, 1L, 1L),
.Label = "2016-10-19 00:00:00",
class = "factor")),
.Names = "Datetime",
row.names = c(NA, -6L),
class = "data.frame")
t$Datetime <- as.POSIXct(t$Datetime, format="%Y-%m-%d %H:%M:%S")
head(t, 3)
# Datetime
# 1 2016-10-19 01:01:01
# 2 2016-10-19 01:01:01
# 3 2016-10-19 01:01:01
str(t)
#'data.frame': 6 obs. of 1 variable:
# $ Datetime: POSIXct, format: "2016-10-19 01:01:01" "2016-10-19 01:01:01" ...
library(lubridate)
t$Datetime <- ymd_hms(t$Datetime)