1

我有一个包含 12 个数据框的列表,每个数据框都有 9 列。行数可变。现在我想遍历列表并获取每个数据帧的列的五个最大值Sum_of_weights

如果我将列表导入我的环境并使用它,我只能解决我的问题,但必须有一个更优雅的解决方案。我认为这应该是可能的lapply,但我无法理解。

每个 df 看起来都是这样的:

 $ Utilities                 :'data.frame': 28 obs. of  10 variables:
 .. $ Price_to_Book      : num [1:28] 164 177 102 118 199 191 245 99 287 126 ...
 .. $ Price_Earnings     : num [1:28] 39 253 202 272 361 178 303 212 301 215 ...
 .. $ Dividend_Yield     : num [1:28] 475 427 441 433 254 494 394 443 444 409 ...
 .. $ Free_Cashflow_Yield: num [1:28] 63 67 98 145 80 188 95 71 62 83 ...
 .. $ Operation_Margin   : num [1:28] 229 286 257 355 425 204 311 329 435 247 ...
 .. $ Debt_to_Equity     : num [1:28] 480 312 320 327 356 430 425 311 426 314 ...
 .. $ Earnings_Growth    : num [1:28] 237 235 214 131 249 368 134 141 223 180 ...
 .. $ Return on_Capital  : num [1:28] 123 186 168 187 162 191 158 165 176 156 ...
 .. $ Sum_of_weights     : num [1:28] 1810 1943 1802 1968 2086 ...
 .. $ Sector             : Factor w/ 11 levels "Consumer Discretionary",..: 11 11 11 11 11 11 11 11 11 11 ...
 .. ..- attr(*, "names")= chr [1:28] "AES.CORP" "ALLIANT.ENERGY.CORP" "AMEREN.CORPORATION" "AMERICAN.ELECTRIC.POWER" ...
4

2 回答 2

2

就像是

library(dplyr);library(purrr)
your_list %>% map(top_n, 5, sum_of_weights)

可能会这样做。

于 2018-08-20T17:50:27.010 回答
1

这应该有效:

仅值

lapply(your_list, function(d) head(d$sum_of_weights[order(-d$sum_of_weights)], 5))

使用行名:

lapply(your_list, function(d) head(d["sum_of_weights"][order(-d$sum_of_weights)], 5))

整行:

lapply(your_list, function(d) head(d[order(-d$sum_of_weights), ], 5))
于 2018-08-20T17:51:53.047 回答