将 xts 转换为 ts:回合误差(频率)
我有一些导入的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
strptime
创建一个POSIXlt
类的对象。as.ts
不支持它,并认为它是一个列表,因此抱怨非数字参数。改为转换为POSIXct
。strptime
creates an object of classPOSIXlt
.as.ts
doesn't support it, and thinks it is a list, hence the complaint about a non-numeric argument. Convert toPOSIXct
instead.xts/zoo 对象必须是规则的才能具有非 NULL 频率。
您没有显示如何更改为每秒分辨率,但如果您通过
options(digits.secs=0)
尝试,则不会起作用,因为它只会影响打印。你需要做这样的事情: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: