R 从列表中的数据帧列中获取五个最大值

R 从列表中的数据帧列中获取五个最大值,r,R,我有一个包含12个数据帧的列表,每个数据帧有9列。行数为。 现在,我想对列表进行评分,并为每个数据帧获取列权重之和的五个最大值 只有将列表导入到我的环境中并使用它,我才能解决我的问题,但必须有一个更优雅的解决方案。我想用lappy应该是可能的,但我不能动脑 每个df看起来都是这样的: $ Utilities :'data.frame': 28 obs. of 10 variables: .. $ Price_to_Book : num [1:28]

我有一个包含12个数据帧的列表,每个数据帧有9列。行数为。 现在,我想对列表进行评分,并为每个数据帧获取列
权重之和的五个最大值

只有将列表导入到我的环境中并使用它,我才能解决我的问题,但必须有一个更优雅的解决方案。我想用
lappy
应该是可能的,但我不能动脑

每个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" ...
差不多

library(dplyr);library(purrr)
your_list %>% map(top_n, 5, sum_of_weights)
可能就可以了。

这应该可以:

价值观

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))

非常感谢你!很好用。如果它也能告诉我索引,那就更好了,但我相信我会找到办法的。再次感谢只要在问题中问出你想要什么,你就可能得到它。谢谢。我真的很感谢你的努力!在计算上,编写
d[head(order(…),]
更有效,因此只需对data.frame的相关子集进行排序。非常感谢!同样有效,甚至可以打印整行!以前从未使用过purrr,但似乎是一个方便的软件包。非常感谢。请注意,
top\u n
head(order())
以不同的方式处理关系,例如,
tibble(x=rep(1,10))%%>%top\u n(6)
返回所有10行,而
head(order())
返回前6行