r - 数据框列表列表
问题描述
我有一个名为“mylist”的列表。它包含 2 个项目。这些项目中的每一项都是数据框的列表。列表的第一项是 1 个数据框的列表,第二项是 2 个数据框的列表,如下所示:-
str(mylist1)
List of 1
$ :'data.frame': 3 obs. of 3 variables:
..$ employee : chr [1:3] "John Doe" "Peter Gynn" "Jolie Hope"
..$ salary : num [1:3] 21000 23400 26800
..$ startdate: Date[1:3], format: "2010-11-01" "2008-03-25" "2007-03-14"
> str(mylist2)
List of 2
$ :'data.frame': 3 obs. of 3 variables:
..$ employee : chr [1:3] "John Doe" "Peter Gynn" "Jolie Hope"
..$ salary : num [1:3] 21000 23400 26800
..$ startdate: Date[1:3], format: "2010-11-01" "2008-03-25" "2007-03-14"
$ :'data.frame': 3 obs. of 3 variables:
..$ employee : chr [1:3] "John Doe1" "Peter Gynn1" "Jolie Hope1"
..$ salary : num [1:3] 20000 25000 30000
..$ startdate: Date[1:3], format: "2011-11-01" "2009-03-25" "2008-03-14"
> str(mylist)
List of 2
$ :List of 1
..$ :'data.frame': 3 obs. of 3 variables:
.. ..$ employee : chr [1:3] "John Doe" "Peter Gynn" "Jolie Hope"
.. ..$ salary : num [1:3] 21000 23400 26800
.. ..$ startdate: Date[1:3], format: "2010-11-01" "2008-03-25" "2007-03-14"
$ :List of 2
..$ :'data.frame': 3 obs. of 3 variables:
.. ..$ employee : chr [1:3] "John Doe" "Peter Gynn" "Jolie Hope"
.. ..$ salary : num [1:3] 21000 23400 26800
.. ..$ startdate: Date[1:3], format: "2010-11-01" "2008-03-25" "2007-03-14"
..$ :'data.frame': 3 obs. of 3 variables:
.. ..$ employee : chr [1:3] "John Doe1" "Peter Gynn1" "Jolie Hope1"
.. ..$ salary : num [1:3] 20000 25000 30000
.. ..$ startdate: Date[1:3], format: "2011-11-01" "2009-03-25" "2008-03-14"
列表本身看起来像这样:-
mylist1
[[1]]
employee salary startdate
1 John Doe 21000 2010-11-01
2 Peter Gynn 23400 2008-03-25
3 Jolie Hope 26800 2007-03-14
> mylist2
[[1]]
employee salary startdate
1 John Doe 21000 2010-11-01
2 Peter Gynn 23400 2008-03-25
3 Jolie Hope 26800 2007-03-14
[[2]]
employee salary startdate
1 John Doe1 20000 2011-11-01
2 Peter Gynn1 25000 2009-03-25
3 Jolie Hope1 30000 2008-03-14
> mylist
[[1]]
[[1]][[1]]
employee salary startdate
1 John Doe 21000 2010-11-01
2 Peter Gynn 23400 2008-03-25
3 Jolie Hope 26800 2007-03-14
[[2]]
[[2]][[1]]
employee salary startdate
1 John Doe 21000 2010-11-01
2 Peter Gynn 23400 2008-03-25
3 Jolie Hope 26800 2007-03-14
[[2]][[2]]
employee salary startdate
1 John Doe1 20000 2011-11-01
2 Peter Gynn1 25000 2009-03-25
3 Jolie Hope1 30000 2008-03-14
如果我要将列表“mylist”分配给这样的变量:-
testvar <- mylist
命令 :-
str(测试变量)
正确给出以下输出。
List of 2
$ :List of 1
..$ :'data.frame': 3 obs. of 3 variables:
.. ..$ employee : chr [1:3] "John Doe" "Peter Gynn" "Jolie Hope"
.. ..$ salary : num [1:3] 21000 23400 26800
.. ..$ startdate: Date[1:3], format: "2010-11-01" "2008-03-25" "2007-03-14"
$ :List of 2
..$ :'data.frame': 3 obs. of 3 variables:
.. ..$ employee : chr [1:3] "John Doe" "Peter Gynn" "Jolie Hope"
.. ..$ salary : num [1:3] 21000 23400 26800
.. ..$ startdate: Date[1:3], format: "2010-11-01" "2008-03-25" "2007-03-14"
..$ :'data.frame': 3 obs. of 3 variables:
.. ..$ employee : chr [1:3] "John Doe1" "Peter Gynn1" "Jolie Hope1"
.. ..$ salary : num [1:3] 20000 25000 30000
.. ..$ startdate: Date[1:3], format: "2011-11-01" "2009-03-25" "2008-03-14"
但是以下命令给出了错误:-
str(get(paste0("testvar", "[[1]]")))
错误
Error in get(paste0("testvar", "[[1]]")) :
object 'testvar[[1]]' not found
为什么上面的命令没有找到实际上是列表'mylist'的testvar对象。我希望能够获得列表“mylist”的第一项的结构(甚至类)。我需要以编程方式进行,并且不能对其进行硬编码。
请问有什么建议吗?
此致
迪帕克
解决方案
get
或mget
仅返回在全局环境中创建的对象/对象。 "testvar"
是一个使用值创建的对象,而 "testvar[[1]]" 不是对象标识符,它只是list
testvar
. 因此,我们get
使用对象标识符的值并提取list
元素[[
get("testvar")[[1]]
它类似于获取 data.frame 的列
data(mtcars)
get("mtcars") # // => works
get("mtcars[[1]]") # // => returns error
获取错误(“mtcars [[1]]”):找不到对象'mtcars [[1]]'
目前尚不清楚为什么我们需要使用get
. 如果打算遍历mylist
,则可以使用lapply
lapply(mylist, function(innerlst) yourfun(innerlst))
推荐阅读
- c# - 当文本框中的字段不正确时如何显示两个不同的消息框
- spring-cloud-stream - 带有 kinesis 的 Spring 云流:AmazonDynamoDBLockClient 在循环中抛出异常
- flutter - Flutter - 占据整个屏幕宽度的图像
- eclipse-plugin - 如何动态更新有助于 org.eclipse.ui.menus 扩展的菜单标签
- powershell - 如何强制函数返回单个元素数组而不是包含的对象?
- c# - ASP.NET Core 服务未创建 RabbitMQ 队列
- python - 如何根据特定条件创建一列特定值的零?
- pdf - 试图理解 PDF 中交叉引用 (XRef) 流中的数据
- android - 到达底部后进行视图滚动
- apache-spark-sql - Spark SQL 性能调优