简体   繁体   English

R中的时间序列数据中的时间戳转换

[英]Timestamp conversion in Time series data in R

I am new to R, trying to analyse a time series data from HVAC sensor. 我是R的新手,试图分析HVAC传感器的时间序列数据。 The data in in the following format. 数据格式如下。 Do I need to convert it to a ts() object before staring any analysis or can I gp ahead with the same below are the results found: 在凝视任何分析之前是否需要将其转换为ts()对象,还是可以在gp前面进行以下操作,找到以下结果:

 Time | Value
1 7/6/16 4:45|28
2 7/8/16 4:51|31
3 7/6/16 5:10|26
4 7/6/16 5:10|26
5 7/6/16 5:11|26
6 7/6/16 5:11|26

str(dataset)
data.frame':    628 obs. of  2 variables:
 $ Time : chr  "7/6/16 4:45" "7/8/16 4:51" "7/6/16 5:10" "7/6/16 5:10" ...
 $ Value: int  28 31 26 26 26 26 26 27 29 29 ... 

Now do I need to change the time format from character to any other format? 现在我需要将时间格式从字符更改为任何其他格式吗? When I am running the following code: 当我运行以下代码时:

dataset$Time=as.POSIXct(as.character(dataset$Time),format="%Y-%m-%d %H:%M:%S")

the time is becoming NA in the dataset. 时间在数据集中变成NA

Thanks Dwiti 谢谢杜威蒂

Try this (assuming you have date in m/d/y format) 尝试此操作(假设您使用m / d / y格式的日期)

dataset$Time = strptime(as.character(dataset$Time),format="%m/%d/%y %H:%M")
head(dataset$Time,3)
[1] "2016-07-06 04:45:00 IST" "2016-07-08 04:51:00 IST" "2016-07-06 05:10:00 IST"

We can use compact option mdy_hm from lubridate package 我们可以使用lubridate包中的紧凑选项mdy_hm

library(lubridate)
dataset$Time <- mdy_hm(dataset$Time)
dataset$Time
#[1] "2016-07-06 04:45:00 UTC" "2016-07-08 04:51:00 UTC"
#[3] "2016-07-06 05:10:00 UTC" "2016-07-06 05:10:00 UTC"
#[5] "2016-07-06 05:11:00 UTC" "2016-07-06 05:11:00 UTC"

data 数据

dataset <- structure(list(Time = c("7/6/16 4:45", "7/8/16 4:51", "7/6/16 5:10", 
"7/6/16 5:10", "7/6/16 5:11", "7/6/16 5:11"), Value = c(28L, 
31L, 26L, 26L, 26L, 26L)), .Names = c("Time", "Value"), row.names = c(NA, 
-6L), class = "data.frame")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM