我不能把数字变成时间。

时间:2022-07-11 15:22:08

Here's the structure of the data

这是数据的结构。

我不能把数字变成时间。

What I want to do is to change the form of Time into POSIXct.

我想做的是把时间的形式变成POSIXct。

Here's what I have tried.

这是我所尝试的。

a <- as.POSIXct(power_consumption$Time, "%H:%M:%S")
Error in as.POSIXlt.character(as.character(x), ...) : 
  字符串的格式不够标准明确(The format of the string is not standard enough)

a <- as.POSIXlt(power_consumption$Time, "%H:%M:%S")
Error in as.POSIXlt.character(as.character(x), ...) : 
  字符串的格式不够标准明确(The format of the string is not standard enough) 

a <- strptime(power_consumption$Time, "%H:%M:%S")
head(a)
[1] NA                        "2017-07-07 17:24:00 CST" "2017-07-07 17:25:00 CST" "2017-07-07 17:26:00 CST" "2017-07-07 17:27:00 CST"
[6] "2017-07-07 17:28:00 CST"

I don't think the result is right, for the struction of Time has time only.

我不认为结果是正确的,因为时间的结构只有时间。

What should I do to transform Time into %H:%M:%S? Thanks

我该怎么做才能把时间转换成%H:%M:%S?谢谢

2 个解决方案

#1


1  

By definition POSIX time formats must have a date associated with them. If the date doesn't matter, you can proceed as is, and specify an origin date if you care which will set everything to an arbitrary date you provide.

根据定义,POSIX时间格式必须有一个与之相关联的日期。如果日期不重要,您可以按原样进行,并指定一个起始日期,如果您关心它将把一切设置为您提供的任意日期。

You can follow @HubertL's advice and construct the full POSIX object, which seems like the most logical option.

您可以遵循@HubertL的建议并构建完整的POSIX对象,这似乎是最合理的选择。

Alternatively you can use chron::times() to simply store everything in an HH:MM:SS object. There's also hms::as.hms()

或者您也可以使用chron::times()来简单地将所有东西存储在HH:MM:SS对象中。还有hms:as.hms()

Unless you have a specific reason why you need to keep date and time separate, just construct the full POSIX date/time object.

除非你有一个明确的理由,为什么你需要把日期和时间分开,只需要构造一个完整的POSIX日期/时间对象。

Also, I should add that by default, POSIXct/lt will assign a date (in this case today's) if you don't provide one, hence all of yours saying 2017-07-07.

此外,我还应该加上默认的,POSIXct/lt将指定一个日期(在本例中为今天的日期),如果您不提供一个日期,因此您的所有人都说“2017-07-07”。

#2


0  

@Ryan You need to reload the data frame from the source and set each column to the correct data type. Everything inpower_consumption should be a double except the date and time. You will be unable to do any serious processing, because all the data is represented as factors.

您需要从源重新加载数据框架,并将每个列设置为正确的数据类型。除了日期和时间之外,所有的inpower_consumption都应该是一个double。您将无法进行任何认真的处理,因为所有的数据都表示为因子。

#1


1  

By definition POSIX time formats must have a date associated with them. If the date doesn't matter, you can proceed as is, and specify an origin date if you care which will set everything to an arbitrary date you provide.

根据定义,POSIX时间格式必须有一个与之相关联的日期。如果日期不重要,您可以按原样进行,并指定一个起始日期,如果您关心它将把一切设置为您提供的任意日期。

You can follow @HubertL's advice and construct the full POSIX object, which seems like the most logical option.

您可以遵循@HubertL的建议并构建完整的POSIX对象,这似乎是最合理的选择。

Alternatively you can use chron::times() to simply store everything in an HH:MM:SS object. There's also hms::as.hms()

或者您也可以使用chron::times()来简单地将所有东西存储在HH:MM:SS对象中。还有hms:as.hms()

Unless you have a specific reason why you need to keep date and time separate, just construct the full POSIX date/time object.

除非你有一个明确的理由,为什么你需要把日期和时间分开,只需要构造一个完整的POSIX日期/时间对象。

Also, I should add that by default, POSIXct/lt will assign a date (in this case today's) if you don't provide one, hence all of yours saying 2017-07-07.

此外,我还应该加上默认的,POSIXct/lt将指定一个日期(在本例中为今天的日期),如果您不提供一个日期,因此您的所有人都说“2017-07-07”。

#2


0  

@Ryan You need to reload the data frame from the source and set each column to the correct data type. Everything inpower_consumption should be a double except the date and time. You will be unable to do any serious processing, because all the data is represented as factors.

您需要从源重新加载数据框架,并将每个列设置为正确的数据类型。除了日期和时间之外,所有的inpower_consumption都应该是一个double。您将无法进行任何认真的处理,因为所有的数据都表示为因子。