简体   繁体   中英

JSON subsetting unique dates in R

I am having difficulty subsetting JSON data. I am fairly new to R understand the basics of subsetting.

    library(jsonlite)
    library(curl)

    url1="https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=RCS&outputsize=full&apikey=DEMO"

    StockData2<- fromJSON(url1,flatten = TRUE)

Data looks like this coming in (2 days worth out of 4394 days). The trouble I am having is each date is unique and I can't figure out how to pull just the date and one of its subsetted prices out.

> str(StockData)
List of 2
 $ Meta Data          :List of 5
  ..$ 1. Information   : chr "Daily Time Series with Splits and Dividend Events"
  ..$ 2. Symbol        : chr "RCS"
  ..$ 3. Last Refreshed: chr "2017-06-20 10:27:00"
  ..$ 4. Output Size   : chr "Full size"
  ..$ 5. Time Zone     : chr "US/Eastern"
 $ Time Series (Daily):List of 4394
  ..$ 2017-06-20 10:27:00:List of 8
  .. ..$ 1. open             : chr "10.1100"
  .. ..$ 2. high             : chr "10.1600"
  .. ..$ 3. low              : chr "10.0400"
  .. ..$ 4. close            : chr "10.1100"
  .. ..$ 5. adjusted close   : chr "10.1100"
  .. ..$ 6. volume           : chr "17776"
  .. ..$ 7. dividend amount  : chr "0.00"
  .. ..$ 8. split coefficient: chr "1.0000"
  ..$ 2017-06-19         :List of 8
  .. ..$ 1. open             : chr "9.9200"
  .. ..$ 2. high             : chr "10.1200"
  .. ..$ 3. low              : chr "9.9200"
  .. ..$ 4. close            : chr "10.0800"
  .. ..$ 5. adjusted close   : chr "10.0800"
  .. ..$ 6. volume           : chr "160599"
  .. ..$ 7. dividend amount  : chr "0.00"
  .. ..$ 8. split coefficient: chr "1.0000"

Daily<-StockData$`Time Series (Daily)`

Will produce a large list of 4394 elements. How do I get just "dates" and "adjusted close" out of this list?

Now I can get the data I have a full solution. My earlier answer, which used as.data.frame , failed on the real data (but worked on my test) because of the way that as.data.frame tried to get the column names from the expression passed in. For some rare combination of data names and types that your data triggered, as.data.frame ended up trying to set the wrong number of column names. Using data.frame instead, or adding the optional=FALSE argument to as.data.frame fixes this.

So the solution is:

result = setNames(
   data.frame(
     sapply(
       StockData2[["Time Series (Daily)"]],
         function(x){
           x[["5. adjusted close"]]
            })),
   c("adjusted close"))

giving:

> head(result)
           adjusted close
2017-06-21        70.2700
2017-06-20        69.9100
2017-06-19        70.8700
2017-06-16        70.0000
2017-06-15        69.9000
2017-06-14        70.2700

If you are interested in what caused the fail, read on...

Set up two very similar lists:

> Xnum = list(list(x=1),list(x=2))
> Xchar = list(list(x="1"),list(x="2"))

Convert x bits to a data frame. For the numbers, it works, but as you see the column name is a bit long, but no problem:

> as.data.frame(sapply(Xnum,function(f){f$x}))
  sapply(Xnum, function(f) {     f$x })
1                                     1
2                                     2

But for the character one, it gets in a real mess:

> as.data.frame(sapply(Xchar,function(f){f$x}))
Error in as.data.frame.vector(x, ..., nm = nm) : 
  'names' attribute [3] must be the same length as the vector [1]

because it ends up splitting the sapply(Xchar,function(f){f$x}) expression into three parts. I don't know why the numeric version doesn't fall foul to this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM