本文介绍了应用于数据帧列表(R)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的工作区中,我有 dfo ,它是216个对象的列表。每个对象都是一个数据帧,显示前两个对象:

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 

我也有一个数据帧 index ,其中有216行2列:

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

对于列表 dfo 中的每个数据框,我想将向量 dfo $ K 除以相应的该日期的 index $ Index 值。 dfo 数据框列表中的216个日期和 index 数据框中的216个日期完全对应,但是我已在 dfo index 中的 Date 列中冗余。

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.

在这种情况下,如何实施 lapply ?我不太了解如何将216个数据帧的列表与216行的数据帧接口。

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.

推荐答案

考虑遍历数据框列表的每个对象并与 index 按日期显示数据框。然后在循环中,运行计算 K /索引,保留 Index ,然后返回结果。

Consider iterating through each object of data frame list and merge with index data frame by the date. Then in the loop, run your calculation K / Index, keep Index or not, and then return the result.

newdfList <- lapply(dfList, function(df) {                             
                        newdf <- merge(ind, df, by='Date')
                        newdf['K'] <- newdf['K'] / newdf['Index']                            
                        newdf['Index'] <- NULL
                        return(newdf)
                    })

这篇关于应用于数据帧列表(R)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 16:25