Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting xts to ts: Error in Round(frequency)

Tags:

r

time-series

xts

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.

like image 868
Kyle Brandt Avatar asked Oct 10 '22 11:10

Kyle Brandt


2 Answers

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"))
like image 180
Richie Cotton Avatar answered Oct 17 '22 13:10

Richie Cotton


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
like image 23
Joshua Ulrich Avatar answered Oct 17 '22 13:10

Joshua Ulrich