0

I have a list in R that I want to loop through all the elements.

This is the structure of the object:

> str(AAPL.OPT[c])
List of 1
 $ jun.12.2020:List of 2
  ..$ calls:'data.frame':   52 obs. of  7 variables:
  .. ..$ Strike: num [1:52] 180 185 200 210 240 ...
  .. ..$ Last  : num [1:52] 123 118 131 120 85 ...
  .. ..$ Chg   : num [1:52] 0 0 7.61 9.48 0 ...
  .. ..$ Bid   : num [1:52] 149 144 129 119 89 ...
  .. ..$ Ask   : num [1:52] 153.3 148.5 133.5 123.7 93.5 ...
  .. ..$ Vol   : int [1:52] NA 15 16 2 1 1 3 36 1 2 ...
  .. ..$ OI    : int [1:52] 0 15 25 4 50 3 4 36 6 10 ...
  ..$ puts :'data.frame':   56 obs. of  7 variables:
  .. ..$ Strike: num [1:56] 150 165 170 180 185 190 195 200 205 210 ...
  .. ..$ Last  : num [1:56] 0.05 0.02 0.14 0.05 0.03 0.02 0.01 0.02 0.01 0.01 ...
  .. ..$ Chg   : num [1:56] 0 0 0 0 0 0 0 0 0 0 ...
  .. ..$ Bid   : num [1:56] NA 0 0 0 0 0 0 0 0 0 ...
  .. ..$ Ask   : num [1:56] 2.13 0.11 0.11 1.8 1.87 0.01 1.88 0.5 1.88 2.13 ...
  .. ..$ Vol   : int [1:56] NA 1 1 2 1 16 1 17 1 21 ...
  .. ..$ OI    : int [1:56] 1 10 7 9 76 201 113 314 92 264 ...

I cannot access the next level of the object programatically (by indexing the value)

I want to do something like this:

AAPL.OPT[c][1]

instead of this

AAPL.OPT[c]$jun.12.2020

Sample data of AAPL.OPT[c]

$`jun.12.2020`$`calls`
                    Strike   Last        Chg    Bid    Ask   Vol   OI
AAPL200612C00180000  180.0 123.29 0.00000000 149.00 153.35    NA    0
AAPL200612C00185000  185.0 117.60 0.00000000 144.00 148.50    15   15
AAPL200612C00200000  200.0 131.15 7.60999300 129.00 133.50    16   25
AAPL200612C00210000  210.0 119.95 9.47999600 119.30 123.65     2    4
....
1
  • I think AAPL.OPT[[c]][[1]] should give you AAPL.OPT[c]$jun.12.2020 Commented Jun 7, 2020 at 4:33

2 Answers 2

2

AAPL.OPT[c] gives a list of length 1 which has two other lists in them. If we use [[c]] it gives a list of length 2 andtTo access each dataframe you can subset them further using [[ so AAPL.OPT[[c]][[1]] and AAPL.OPT[[c]][[2]].

Sign up to request clarification or add additional context in comments.

Comments

1

We can use

 AAPL.OPT[[c]]$jun.12.2020

1 Comment

It helps more if you supply an explanation why this is the preferred solution and explain how it works. We want to educate, not just provide code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.