将 xts 转换为 ts:回合误差(频率)

发布于 2024-11-25 02:07:10 字数 2030 浏览 1 评论 0原文

我有一些导入的 csv 数据,我已将它们转换为 xts 对象。如果我尝试将其转换为 ts 对象(最终目标是使用 acf 等函数),我会得到:

“轮次错误(频率):数学的非数字参数 函数”

转换它的代码是:

library("zoo")
#Working With Milliseconds
op <- options(digits.secs=3)

#Rename Function
clean_perfmon = function(x, servername) {
  names(x)[names(x)=="X.PDH.CSV.4.0...Coordinated.Universal.Time..0."] <- "Time"
  x$Time = strptime(x$Time, "%m/%d/%Y %H:%M:%OS")
  return(x)
}

web02 = read.csv("/home/kbrandt/Desktop/Shared/web02_2011_07_20_1.csv")
web02 = clean_perfmon(web02, "NY.WEB02")
web02ts = xts(web02[,-1], web02[,"Time"])

时间大部分是规则的,但 MS 有一些变化:

time(web02ts)[1:3]
[1] "2011-07-20 11:21:50.459 EDT" "2011-07-20 11:21:51.457 EDT" "2011-07-20 11:21:52.456 EDT"

一些数据有 NA 点:

> web02ts[1:3,1]
                        X..NY.WEB02.Process.Idle....Processor.Time
2011-07-20 11:21:50.459                                         NA
2011-07-20 11:21:51.457                                   1134.819
2011-07-20 11:21:52.456                                   1374.877

更新:
更改为每秒分辨率,并且非 na 子集没有帮助:

> as.ts(web02ts[2:10,1])
Error in round(frequency) : Non-numeric argument to mathematical function
> web02ts[2:10,1]
                    X..NY.WEB02.Process.Idle....Processor.Time
2011-07-20 11:21:51                                   1134.819
2011-07-20 11:21:52                                   1374.877
2011-07-20 11:21:53                                   1060.842
2011-07-20 11:21:54                                   1067.092
2011-07-20 11:21:55                                   1195.205
2011-07-20 11:21:56                                   1223.328
2011-07-20 11:21:57                                   1121.774
2011-07-20 11:21:58                                   1187.393
2011-07-20 11:21:59                                   1378.001
>

此外,Frequency(web02ts) 返回 NULL

I have some imported csv data that I have turned into an xts object. If I try to convert it into a ts object (with the end goal of using functions like acf) I get:

"Error in round(frequency) : Non-numeric argument to mathematical
function"

The code to convert it is:

library("zoo")
#Working With Milliseconds
op <- options(digits.secs=3)

#Rename Function
clean_perfmon = function(x, servername) {
  names(x)[names(x)=="X.PDH.CSV.4.0...Coordinated.Universal.Time..0."] <- "Time"
  x$Time = strptime(x$Time, "%m/%d/%Y %H:%M:%OS")
  return(x)
}

web02 = read.csv("/home/kbrandt/Desktop/Shared/web02_2011_07_20_1.csv")
web02 = clean_perfmon(web02, "NY.WEB02")
web02ts = xts(web02[,-1], web02[,"Time"])

The time is mostly regular, but with some variation in the MS:

time(web02ts)[1:3]
[1] "2011-07-20 11:21:50.459 EDT" "2011-07-20 11:21:51.457 EDT" "2011-07-20 11:21:52.456 EDT"

Some of the data has NA points:

> web02ts[1:3,1]
                        X..NY.WEB02.Process.Idle....Processor.Time
2011-07-20 11:21:50.459                                         NA
2011-07-20 11:21:51.457                                   1134.819
2011-07-20 11:21:52.456                                   1374.877

Update:
Changing to per second resolution, and a non-na subset doesn't help:

> as.ts(web02ts[2:10,1])
Error in round(frequency) : Non-numeric argument to mathematical function
> web02ts[2:10,1]
                    X..NY.WEB02.Process.Idle....Processor.Time
2011-07-20 11:21:51                                   1134.819
2011-07-20 11:21:52                                   1374.877
2011-07-20 11:21:53                                   1060.842
2011-07-20 11:21:54                                   1067.092
2011-07-20 11:21:55                                   1195.205
2011-07-20 11:21:56                                   1223.328
2011-07-20 11:21:57                                   1121.774
2011-07-20 11:21:58                                   1187.393
2011-07-20 11:21:59                                   1378.001
>

Also, frequency(web02ts) returns NULL.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光瘦了 2024-12-02 02:07:10

strptime 创建一个 POSIXlt 类的对象。 as.ts 不支持它,并认为它是一个列表,因此抱怨非数字参数。改为转换为 POSIXct

as.POSIXct(strptime(x$Time, "%m/%d/%Y %H:%M:%OS"))

strptime creates an object of class POSIXlt. as.ts doesn't support it, and thinks it is a list, hence the complaint about a non-numeric argument. Convert to POSIXct instead.

as.POSIXct(strptime(x$Time, "%m/%d/%Y %H:%M:%OS"))
雪化雨蝶 2024-12-02 02:07:10

xts/zoo 对象必须是规则的才能具有非 NULL 频率。

您没有显示如何更改为每秒分辨率,但如果您通过 options(digits.secs=0) 尝试,则不会起作用,因为它只会影响打印。你需要做这样的事情:

# example data
set.seed(21)
web02ts <- xts(rnorm(10), Sys.time()+1:10+runif(10)/3)

web02ts_reg <- align.time(web02ts,1)
frequency(web02ts_reg)
# [1] 1
as.ts(web02ts_reg)
# Time Series:
# Start = 1 
# End = 10 
# Frequency = 1 
#  [1]  0.793013171  0.522251264  1.746222241 -1.271336123  2.197389533
#  [6]  0.433130777 -1.570199630 -0.934905667  0.063493345 -0.002393336

A xts/zoo object must be regular to have a non-NULL frequency.

You don't show how you changed to per-second resolution but if you tried via options(digits.secs=0), that won't work because it only affects printing. You would need to do something like this:

# example data
set.seed(21)
web02ts <- xts(rnorm(10), Sys.time()+1:10+runif(10)/3)

web02ts_reg <- align.time(web02ts,1)
frequency(web02ts_reg)
# [1] 1
as.ts(web02ts_reg)
# Time Series:
# Start = 1 
# End = 10 
# Frequency = 1 
#  [1]  0.793013171  0.522251264  1.746222241 -1.271336123  2.197389533
#  [6]  0.433130777 -1.570199630 -0.934905667  0.063493345 -0.002393336
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文