[R] List / Matrix to Data Frame

Sparks, John j@p@rk4 @end|ng |rom u|c@edu
Fri Jul 2 02:35:09 CEST 2021


Hi R-Helpers,

I am taking it upon myself to delve into the world of lists for R.  In no small part because I appear to have discovered a source of data for an exceptionally good price but that delivers much of that data in json format.

So over the last day or so I managed to fight the list processing tools to a draw and get a list that has only selected elements (actually it ends up in matrix form).  But when I try to convert that to a data frame I can't get it to a form that is workable.

I have visited some pages about converting a matrix to a data frame but they result in highly redundant and inelegant data.

I am thinking that someone who works with lists and matrices knows how to do this quite easily and would be willing to provide a solution.

The reproducible example is shown below.  Just to be explicit, what I am trying to get to is something along the lines of a data frame like this.

Date              netIncome      Gross Profit
2020-09-30 57411000000 104956000000
2019-09-30 55256000000   98392000000

.....

The closest I get is a matrix that looks like this

> wanted
            2020-09-30        2019-09-30       2018-09-30        2017-09-30       2016-09-30       2015-09-30       2014-09-30
date        "2020-09-30"      "2019-09-30"     "2018-09-30"      "2017-09-30"     "2016-09-30"     "2015-09-30"     "2014-09-30"
netIncome   "57411000000.00"  "55256000000.00" "59531000000.00"  "48351000000.00" "45687000000.00" "53394000000.00" "39510000000.00"
grossProfit "104956000000.00" "98392000000.00" "101839000000.00" "88186000000.00" "84263000000.00" "93626000000.00" "70537000000.00"

Code for example

library(jsonlite)
test <- jsonlite::fromJSON("https://eodhistoricaldata.com/api/fundamentals/AAPL.US?api_token=OeAFFmMliFG5orCUuwAKQ8l4WWFQ67YX")

hist<-test[[13]]
ISY<-hist$Income_Statement$yearly
wanted<-sapply(ISY, "[", j = c("date","netIncome","grossProfit"))


Your guidance would be much appreciated.

--John J. Sparks, Ph.D.

	[[alternative HTML version deleted]]



More information about the R-help mailing list