r - 如何将列表转换为其中一个字段的值向量?
问题描述
l = list()
l[[1]] = list(); l[[1]]$asdf = 'jkl'; l[[1]]$qwerty = 5
l[[2]] = list(); l[[2]]$asdf = 'zxcv'; l[[2]]$qwerty = 55
l[[3]] = list(); l[[3]]$asdf = 'poiu'; l[[3]]$qwerty = 555
现在我想以某种方式qwerty
从这个列表中提取 s ;即我想获得一个等价的,c(5, 55, 555)
因为qwerty
这个列表的连续元素的字段的值是5
, 55
, 555
。
如何获得这个?
(如果我陷入 XY:我真正想要实现的是获取该列表中其qwerty
字段具有最大价值的元素;我认为获得它的一种方法是将 s 提取到我可以调用qwerty
的中间值上)numeric
max
解决方案
我们可以pluck
通过循环遍历“qwerty”list
元素map
library(tidyverse)
map_dbl(l, pluck, "qwerty")
#[1] 5 55 555
或使用sapply
frombase R
sapply(l, `[[`, "qwerty")
#[1] 5 55 555
换max
行以获得最大值
max(sapply(l, `[[`, "qwerty"))
#[1] 555
如果它是元素list
i1 <- which.max(sapply(l, `[[`, "qwerty"))
l[i1]
#[[1]]
#[[1]]$asdf
#[1] "poiu"
#[[1]]$qwerty
#[1] 555
或者另一种选择是
l %>%
transpose %>%
.$qwerty %>%
unlist %>%
which.max %>%
magrittr::extract(l, .)
推荐阅读
- angular - 使用多种颜色的 Angular Material 多进度条
- vlc - 如何在 zk 的 zul 中嵌入 VLC?
- python - 在对第一个 pandas python 进行排序时保持第二级多索引完整
- regex - 检查提供的字符串是否是有效的项目列表
- javascript - 使用尾递归实现javascript函数
- opacity - 康普顿的 i3wm 不透明度
- amazon-s3 - 如何实现对 S3 存储桶中单个对象的访问控制?
- javascript - 如何在 ReactJS 中创建最近几个小时的数组?
- tsql - 在游标中处理具有未知结构的行
- twitter - 将媒体上传到 Twitter 失败