Question

I have a time series json data. I need to calculate the date-value pair between a given pair of dates. I solved the problem using the following way. Here, I need to get the values between 1995-01-01 and 2000-01-01 and it works. But is it possible to do this operation using xts or zoo package? What is the better way? How can i process this type of json values using xts package?

json <-  "[\n {\n \"1986-03-13\": 0.0034722 \n},\n{\n \"1987-09-21\": 0.0069444 \n},\n{\n \"1990-04-16\": 0.013889 \n},\n{\n \"1991-06-27\": 0.020833 \n},\n{\n \"1992-06-15\": 0.03125 \n},\n{\n \"1994-05-23\": 0.0625 \n},\n{\n \"1996-12-09\":  0.125 \n},\n{\n \"1998-02-23\":   0.25 \n},\n{\n \"1999-03-29\":    0.5 \n},\n{\n \"2003-02-18\":      1 \n} \n]"
dates <- c()
values <- c()
for(i in 1:length(fromJSON(json))){
  data <- fromJSON(json)[i]
  date = names(data[[1]])
  value = toString(data[[1]])
  dates <- c(dates,date)
  values <- c(values,value)
}
df <- data.frame(date=dates,value= values)
> df
         date     value
1  1986-03-13 0.0034722
2  1987-09-21 0.0069444
3  1990-04-16  0.013889
4  1991-06-27  0.020833
5  1992-06-15   0.03125
6  1994-05-23    0.0625
7  1996-12-09     0.125
8  1998-02-23      0.25
9  1999-03-29       0.5
10 2003-02-18         1

splDates <- c()
for(date in df$date){
  if(as.Date(date) >= as.Date("1995-01-01") & as.Date(date) <= as.Date("2000-01-01") ){
    splDates <- c(splDates,date) 
  }
}
df1 <- df[which(df$date %in% splDates),]
> df1
        date value
7 1996-12-09 0.125
8 1998-02-23  0.25
9 1999-03-29   0.5
Was it helpful?

Solution

Using the xts package, you can try something like this

require(RJSONIO)
require(xts)

json <-  "[\n {\n \"1986-03-13\": 0.0034722 \n},\n{\n \"1987-09-21\": 0.0069444 \n},\n{\n \"1990-04-16\": 0.013889 \n},\n{\n \"1991-06-27\": 0.020833 \n},\n{\n \"1992-06-15\": 0.03125 \n},\n{\n \"1994-05-23\": 0.0625 \n},\n{\n \"1996-12-09\":  0.125 \n},\n{\n \"1998-02-23\":   0.25 \n},\n{\n \"1999-03-29\":    0.5 \n},\n{\n \"2003-02-18\":      1 \n} \n]"

data <- data.frame(date = unlist(lapply(fromJSON(json), names)),
                   value = unname(unlist(fromJSON(json))),
                   stringsAsFactors = FALSE
                   )

data <- xts(data$value, as.Date(data$date))
data["1995-01-01::2000-01-01"]
##             [,1]
## 1996-12-09 0.125
## 1998-02-23 0.250
## 1999-03-29 0.500

OTHER TIPS

you can convert df to an xts object like this

> df <- as.data.frame(scan(what=list(date="",x=0)))
1: 1986-03-13 0.0034722
2: 1987-09-21 0.0069444
3: 1990-04-16  0.013889
4: 1991-06-27  0.020833
5: 1992-06-15   0.03125
6: 1994-05-23    0.0625
7: 1996-12-09     0.125
8: 1998-02-23      0.25
9: 1999-03-29       0.5
10: 2003-02-18         1
11: 
Read 10 records


> df.xts <- xts(df$x , order.by = as.POSIXlt(df$date , format="%Y-%m-%d"))
> df.xts
                [,1]
1986-03-13 0.0034722
1987-09-21 0.0069444
1990-04-16 0.0138890
1991-06-27 0.0208330
1992-06-15 0.0312500
1994-05-23 0.0625000
1996-12-09 0.1250000
1998-02-23 0.2500000
1999-03-29 0.5000000
2003-02-18 1.0000000
> df.xts["1995::2000"]
            [,1]
1996-12-09 0.125
1998-02-23 0.250
1999-03-29 0.500
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top