Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting an xts object to a useful data frame

Tags:

dataframe

r

xts

A dataframe named d contains this data:

timestamp,value
"2013-06-02 00:00:00",70
"2013-06-02 00:02:00",70
"2013-06-02 00:07:00",60
"2013-06-02 00:15:00",70
"2013-06-02 00:12:00",60
"2013-06-02 00:30:00",70
"2013-06-02 00:45:00",70
"2013-06-02 01:00:00",70

The code I have is:

 d = read.csv(path, header=TRUE, sep=",")
 d2 <- xts(x = d[c("value")], order.by = as.POSIXct(d[, "timestamp"], tz = "GMT", format = "%Y-%m-%d %H:%M:%S"))
ends <- endpoints(d2, on = "minutes", k = 15)
d3   <- period.apply(d2, ends, mean)

After that I want to convert the xts object to the dataframe and I am using this:

d3$timestamp = rownames(d3)
rownames(d3) = NULL
d3$timestamp = strptime(d3$timestamp, "%Y-%m-%d %H:%M:%S")

However in the last step it prints error to this:

Error in NextMethod(.Generic) : 
   number of items to replace is not a multiple of replacement length

As I observe typing d3 after the whole commands the object has this format of data:

                         timestamp
2013-06-02 00:15:00        65
2013-06-02 00:30:00        70
2013-06-02 00:45:00        70
2013-06-02 01:00:00        70

However in the column name it must have the name value and as the second column have the timestamp like here. What could be wrong?

The right output must be this:

      value
        65  2013-06-02 00:15:00
        70  2013-06-02 00:30:00
        70  2013-06-02 00:45:00
        70  2013-06-02 01:00:00 
like image 889
foc Avatar asked Jun 06 '13 13:06

foc


People also ask

How do I convert data to XTS objects in R?

Example: Converting Data Frame to xts / zoo Object frame class to a time series object (i.e. xts or zoo). First, we have to convert our character string variable to the Date class. As you can see, we switched the class from data. frame to xts / zoo.

What is an xts object in R?

xts objects are simple. Think of them as a matrix of observations combined with an index of corresponding dates and times. xts = matrix + times. The main xts constructor takes a number of arguments, but the two most important are x for the data and order.by for the index. x must be a vector or matrix.

Why use xts?

xts makes it easy to join data by column and row using a few different functions. xts objects must be of identical type (e.g. integer + integer), or be POSIXct dates vector, or be atomic vectors of the same type (e.g. numeric), or be a single NA.


1 Answers

You can create your data.frame like this for example :

 data.frame(value=coredata(d3),timestamp=index(d3))
# value           timestamp
# 1    65 2013-06-02 00:12:00
# 2    70 2013-06-02 00:15:00
# 3    70 2013-06-02 00:30:00
# 4    70 2013-06-02 00:45:00
# 5    70 2013-06-02 01:00:00

I advise you also to use read.zoo to read your data as a zoo object and avoid coercing xts by hand. For example:

dat <- read.zoo(text='timestamp,value
"2013-06-02 00:00:00",70
"2013-06-02 00:02:00",70
"2013-06-02 00:07:00",60
"2013-06-02 00:15:00",70
"2013-06-02 00:12:00",60
"2013-06-02 00:30:00",70
"2013-06-02 00:45:00",70
"2013-06-02 01:00:00",70',tz ='' , format = "%Y-%m-%d %H:%M:%S",header=TRUE,
         sep=',')
d2 <- as.xts(dat)
like image 96
agstudy Avatar answered Sep 28 '22 16:09

agstudy