# [R] List / Matrix to Data Frame

Bill Dunlap w||||@mwdun|@p @end|ng |rom gm@||@com
Fri Jul 2 03:01:27 CEST 2021

```Does this do what you want?

> df <- data.frame(check.names=FALSE,
lapply(c(Date="date",netIncome="netIncome",`Gross Profit`="grossProfit"),
function(nm)vapply(ISY, "[[", nm, FUN.VALUE=NA_character_)))
> str(df)
'data.frame':   36 obs. of  3 variables:
\$ Date        : chr  "2020-09-30" "2019-09-30" "2018-09-30" "2017-09-30"
...
\$ netIncome   : chr  "57411000000.00" "55256000000.00" "59531000000.00"
"48351000000.00" ...
\$ Gross Profit: chr  "104956000000.00" "98392000000.00" "101839000000.00"
"88186000000.00" ...
> df\$Date <- as.Date(df\$Date)
> df\$netIncome <- as.numeric(df\$netIncome)
> df\$`Gross Profit` <- as.numeric(df\$`Gross Profit`)
> str(df)
'data.frame':   36 obs. of  3 variables:
\$ Date        : Date, format: "2020-09-30" "2019-09-30" "2018-09-30"
"2017-09-30" ...
\$ netIncome   : num  5.74e+10 5.53e+10 5.95e+10 4.84e+10 4.57e+10 ...
\$ Gross Profit: num  1.05e+11 9.84e+10 1.02e+11 8.82e+10 8.43e+10 ...
> with(df, plot(Date, netIncome))

On Thu, Jul 1, 2021 at 5:35 PM Sparks, John <jspark4 using uic.edu> wrote:

> 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]]
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help