Tuesday, August 28, 2018

dataframe - lapply over list of Data Frames (R)

In my workspace, I have dfo which is a list of 216 objects. Each object is a data frame, the first two objects are shown:



> head(dfo)
$`1997-01-23`
Date C/P K Vol Delta ID
56 1997-01-23 0 400 NA NA 11690674
10 1997-01-23 0 550 NA NA 10376194
34 1997-01-23 0 600 NA NA 11036690
58 1997-01-23 0 650 NA NA 11544898
27 1997-01-23 0 660 NA NA 10759732

52 1997-01-23 0 670 NA NA 11439157
50 1997-01-23 0 680 0.176301 0.995920 11364929
60 1997-01-23 0 690 0.185490 0.990123 11780133
39 1997-01-23 0 700 0.203161 0.972175 11183860
65 1997-01-23 0 710 0.200024 0.955090 11730364
38 1997-01-23 0 720 0.202629 0.923953 10982863
. . . . . . .
. . . . . . .
. . . . . . .
45 1997-01-23 1 785 0.160904 -0.552771 10986679

2 1997-01-23 1 790 0.159603 -0.609276 10333499
23 1997-01-23 1 795 0.156346 -0.666208 10456682
47 1997-01-23 1 800 0.154266 -0.719749 11072475
44 1997-01-23 1 805 0.150034 -0.773075 11165557
63 1997-01-23 1 810 0.151855 -0.812170 11764824
53 1997-01-23 1 815 0.150437 -0.851131 11378977
62 1997-01-23 1 820 NA NA 11532248
18 1997-01-23 1 825 NA NA 10428721
41 1997-01-23 1 830 NA NA 10985583


$`1997-02-20`
Date C/P K Vol Delta ID
125 1997-02-20 0 400 NA NA 11116217
139 1997-02-20 0 450 NA NA 11285261
157 1997-02-20 0 475 NA NA 11697618
100 1997-02-20 0 500 NA NA 10744183
167 1997-02-20 0 525 NA NA 11659969
162 1997-02-20 0 550 NA NA 11774819
79 1997-02-20 0 575 NA NA 10237388
150 1997-02-20 0 600 NA NA 11441546

118 1997-02-20 0 610 NA NA 10875377
72 1997-02-20 0 620 NA NA 10249544
121 1997-02-20 0 625 NA NA 10924970
85 1997-02-20 0 630 NA NA 10387622
102 1997-02-20 0 635 NA NA 10599759
107 1997-02-20 0 640 NA NA 10770025
124 1997-02-20 0 645 NA NA 11068359
129 1997-02-20 0 650 NA NA 10883922
105 1997-02-20 0 660 NA NA 10485716
123 1997-02-20 0 670 NA NA 11020541

175 1997-02-20 0 675 0.244968 0.994066 10350962
98 1997-02-20 0 680 0.261206 0.989390 10574981
. . . . . . .
. . . . . . .
. . . . . . .
99 1997-02-20 1 830 0.182276 -0.719366 10719331
163 1997-02-20 1 840 0.178969 -0.797619 11657641
132 1997-02-20 1 850 0.178679 -0.858147 11205448
169 1997-02-20 1 875 NA NA 11759335
67 1997-02-20 1 900 NA NA 10001169

90 1997-02-20 1 925 NA NA 10196550


I also have a data frame index of 216 rows and 2 columns:



> head(index)
Date Index
1 01/23/1997 776.64
2 02/20/1997 800.35
3 03/20/1997 778.04

4 04/17/1997 760.49
5 05/22/1997 833.86
6 06/19/1997 888.99


For each data frame in list dfo I want to divide the vector dfo$K by the corresponding index$Index value for that date. The 216 dates in the dfo list of data frames and the 216 dates in the index data frame correspond perfectly, but I have included the Date columns in both dfo and index for redundancy.



How would I implement lapply in this case? I do not really understand how to interface a list of 216 data frames with a data frame of 216 rows.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...