从列表列表中提取数据[R] [英] Extract data from list of lists [R]

查看:566
本文介绍了从列表列表中提取数据[R]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要从中提取数据的列表(mydata $ notes)列表.如果我想提取位置",则代码看起来像这样-可以正常工作.

I have a list of lists (mydata$notes) that I want to extract data from. Code looks like this, if I want to extract "location" - this works fine.

location <- unlist (lapply(mydata$notes, function(e) e$location))

现在,我可能要提取更多的变量,例如在原子矢量中提取20个向量,位置","var1","var2","var3"等.

Now, I might have more variables I want to extract, say a vector of 20, "location", "var1", "var2", "var3" and so on, in an atomic vector

names(unlist(mytree$notes[[1]]))

如何循环我的第一个代码以提取此名称变量中给定的所有变量?

How can I loop my first code to extract all variables given in this names-variable?

欢呼

推荐答案

定义一个向量,以保存要提取的列表元素.然后在每个由lapply()调用处理的列表上调用unlist().

Define a vector to hold the list elements which you want to extract. Then call unlist() on each list which is processed by your call to lapply().

vars <- c("location", "var1", "var2", "var3")

location <- unlist (lapply(mydata$notes,
                           function(e) {
                               unlist(e[vars])
                           }))

请注意,我所做的唯一真正的更改是,我没有返回原子向量,而是从每个列表中返回了一个包含多个折叠元素的向量.

Notice that the only real change I made is that instead of returning the atomic vector e$location I instead return a vector consisting of several collapsed elements from each list.

这篇关于从列表列表中提取数据[R]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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