R:将函数应用于数据帧列表中的数据帧列(lapply中的lapply)

时间:2018-06-11 22:28:07

标签: r list dataframe lapply

我试图将一个函数应用于列表中多个数据帧的列。

这似乎应该是一件简单的事情,但在其他类似的例子中,根据每个OP试图完成的事情,答案会很快变得复杂。

以下是使用dput从我的数据中摘录。

l <- structure(list(rent = structure(c(NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, 0.619348507, 0.606042704, 0.69948953, 
0.70404742, 0.715846766, 0.752410211, 0.747648989, 0.731315218, 
0.724050859, 0.731388758, 0.737695122, 0.723440202, 0.672426913, 
0.62184407, 0.579224699, 0.543929258, 0.499494354, 0.482955432, 
0.458398376, 0.518930096, 0.50932028, 0.518930096, 0.525756186, 
0.513746556, 0.503713341, 0.491045528, 0.488167562, 0.476132099, 
0.47692961, 0.477979792, 0.495832538, 0.511954619, 0.53, 0.54, 
0.58, 0.6, 0.59, 0.58, 0.66, 0.64, 0.7, 0.77, 0.8, 0.68, 0.69, 
0.74, 0.69, 0.7, 0.7, 0.658823529, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, 2.094773258, 2.101037984, 2.136784642, 
2.195558442, 2.304390876, 2.428667765, 2.433074558, 2.435306322, 
2.603913629, 2.437131683, 2.400160785, 2.35669125, 2.327183577, 
2.283133019, 2.246607636, 2.212371694, 2.162791522, 2.115152942, 
2.067514362, 2.038931214, 2.000820351, 1.985438936, 1.938178806, 
1.9108904, 1.888022635, 1.843136376, 1.836852022, 1.82947565, 
1.825745961, 1.821102267, 1.834815456, 1.84978194, 1.84, 1.78, 
1.8, 1.88, 1.91, 1.89, 1.94, 1.91, 1.97, 1.89, 2, 2.02, 1.93, 
1.95, 1.95, 1.95, 1.979104478, 1.98880597, NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1.599590974, 1.526601389, 
1.573368264, 1.669687245, 1.653456053, 1.944681138, 2.020075584, 
2.242033637, 2.159612762, 2.025100889, 2.137469624, 2.082928045, 
2.029291987, 1.902079515, 1.842787645, 1.737607978, 1.644962316, 
1.644962316, 1.58757991, 1.511070035, 1.482378831, 1.482378831, 
1.411623672, 1.358613285, 1.327873365, 1.346018731, 1.374365901, 
1.337657385, 1.332574633, 1.329043862, 1.315019275, 1.295720841, 
1.26, 1.23, 1.27, 1.22, 1.1, 1.02, 1.09, 1.03, 1, 0.94, 0.96, 
0.99, 1.01, 0.99, 1, 1.02, 1.02761194, 1.035223881), .Dim = c(65L, 
3L), .Dimnames = list(NULL, c("IndustrialRent", "OfficeRent", 
"RetailRent")), .Tsp = c(2002, 2018, 4), class = c("mts", "ts", 
"matrix")), jobs = structure(c(118700, 116900, 116300, 118400, 
118500, 118400, 117100, 116900, 115400, 115000, 113800, 112500, 
109200, 109000, 108900, 110000, 109300, 109700, 110700, 111300, 
110800, 111200, 110500, 110300, 107700, 107500, 108100, 108900, 
110300, 111700, 112000, 113500, 114000, 114200, 114800, 115100, 
113100, 113300, 113100, 114300, 115300, 116400, 116600, 118100, 
118400, 119500, 120100, 120000, 119000, 119900, 120300, 121400, 
122200, 122600, 123700, 125600, 126100, 127100, 127200, 127900, 
128100, 128400, 128700, 129300, 131200, 131800, 131900, 133400, 
134200, 136000, 136900, 137900, 134700, 134800, 134700, 135700, 
135700, 137300, 138500, 139500, 140300, 140200, 140800, 141600, 
141400, 141900, 144000, 144000, 144500, 146000, 145900, 146900, 
148500, 150000, 151100, 154300, 150500, 151800, 153000, 154600, 
211500, 210000, 209800, 212500, 212100, 212900, 210800, 212700, 
210300, 214300, 214400, 213300, 215400, 213800, 215000, 216700, 
216400, 217200, 216100, 216200, 218500, 220700, 219800, 219800, 
223000, 222900, 221900, 224200, 225200, 225900, 222000, 226700, 
227400, 231100, 230800, 228300, 231400, 231100, 233600, 234800, 
235700, 235500, 231800, 234300, 233400, 237500, 238400, 236800, 
238900, 239800, 241100, 242700, 244700, 245400, 240300, 244900, 
246100, 252400, 252100, 251100, 254000, 253800, 255600, 258700, 
259300, 258200, 257400, 261800, 261800, 268200, 269400, 269200, 
268200, 268000, 269800, 273800, 273400, 275300, 274600, 275900, 
279100, 282700, 282100, 279700, 283000, 281300, 284900, 283700, 
286300, 287500, 285300, 286300, 287500, 289400, 287700, 288000, 
287300, 289200, 288600, 290100, 91600, 90100, 90200, 90600, 91100, 
91600, 92000, 92500, 92200, 93500, 96800, 97600, 92700, 91300, 
91500, 92200, 92700, 93000, 93400, 94000, 94200, 95900, 100100, 
100600, 96500, 94800, 95500, 96000, 96400, 96800, 96500, 96700, 
97200, 98100, 103600, 103000, 98000, 96300, 96400, 96700, 97900, 
98400, 98900, 99400, 99700, 101000, 105800, 106800, 101200, 99800, 
100200, 100500, 101100, 101500, 101600, 102500, 103000, 105400, 
110000, 110900, 105200, 104000, 104700, 105400, 105800, 106000, 
105900, 106600, 106400, 108100, 112100, 112100, 105400, 104600, 
105000, 105600, 106000, 106100, 106300, 107000, 107100, 108800, 
113500, 113200, 107700, 106300, 106100, 106200, 107000, 107000, 
106900, 107300, 107600, 108400, 114500, 114700, 109400, 107000, 
108000, 108000), .Dim = c(100L, 3L), .Dimnames = list(NULL, c("IndustrialUsingJobs", 
"OfficeUsingJobs", "RetailUsingJobs")), .Tsp = c(2010, 2018.25, 
12), class = c("mts", "ts", "matrix"))), .Names = c("rent", "jobs"
))

我想将一个函数应用于列表中每个数据框的每一列。我想出的最直接的方法是:

library(seasonal) #christoph sax's x-13-seats seasonal adjustment implementation
lapply(l, function(x) lapply(x, seas))

此示例不包含任何选项。每列的输出是描述季节性调整的列表。

这段代码的问题在于它相对于我的预期似乎相当慢。是否有更有效和/或更简单的方法来实现这一目标?感谢您的投入。

0 个答案:

没有答案