从嵌套列表中提取具有特定名称的所有元素 [英] pull all elements with specific name from a nested list

查看:50
本文介绍了从嵌套列表中提取具有特定名称的所有元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些存档的 Slack 数据,我正在尝试获取一些关键消息属性.我通过愚蠢地展平整个列表,获得一个 data.frame 或带有嵌套在某些单元格中的列表的 tibble 来做到这一点.随着这个数据集越来越大,我想更聪明地从这个列表中挑选元素,这样当这个缓存变大时,它不会永远用我想要的元素来创建 data.frame 或 tibble.

I have some archived Slack data that I am trying to get some of key message properties. I'd done this by stupidly flattening the entire list, getting a data.frame or tibble with lists nested in some cells. As this dataset gets bigger, I want to pick elements out of this list more smartly so that when this cache becomes big it doesn't take forever to create the data.frame or tibble with the elements I want.

我试图拉取名为type"的所有内容的示例下面进入一个向量或平面列表,我可以将其作为数据帧变量引入.为方便起见,我命名了文件夹和消息级别.任何人都有可以提供帮助的模型代码?

Example where I am trying to pull everything named "type" below into a vector or flat list that I can pull in as a dataframe variable. I named the folder and message level for convenience. Anyone have model code that can help?

library(tidyverse)
    
l <- list(folder_1 = list(
  `msg_1-1` = list(type = "message",
               subtype = "channel_join",
               ts = "1585771048.000200",
               user = "UFUNNF8MA",
               text = "<@UFUNNF8MA> has joined the channel"),
  `msg_1-2` = list(type = "message",
                   subtype = "channel_purpose",
                   ts = "1585771049.000300",
                   user = "UNFUNQ8MA",
                   text = "<@UNFUNQ8MA> set the channel purpose: Talk about xyz")),
  folder_2 = list(
    `msg_2-1` = list(type = "message",
                  subtype = "channel_join",
                  ts = "1585771120.000200",
                  user = "UQKUNF8MA",
                  text = "<@UQKUNF8MA> has joined the channel")) 
)

# gets a specific element
print(l[[1]][[1]][["type"]])

# tried to get all elements named "type", but am not at the right list level to do so
print(purrr::map(l, "type"))

推荐答案

与@Duck & 提供的相关@Abdessabour Mtk 昨天,purrr 有一个函数 map_depth() ,如果您知道它的名称以及它在层次结构中的深度,它将让您获得一个命名属性.在抓取这个大型嵌套列表时非常有用,并且是上面嵌套 map() 调用的更简单的解决方案.

Related to those provided by @Duck & @Abdessabour Mtk yesterday, purrr has a function map_depth() that will let you get a named attribute if you know its name and how deep it is in the hierarchy. REALLY useful when crawling this big nested lists, and is a simpler solution to the nested map() calls above.

purrr::map_depth(l, 2, "type")

这篇关于从嵌套列表中提取具有特定名称的所有元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆