首页 > 解决方案 > 数据框列表列表

问题描述

我有一个名为“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”的第一项的结构(甚至类)。我需要以编程方式进行,并且不能对其进行硬编码。

请问有什么建议吗?

此致

迪帕克

标签: rdataframe

解决方案


getmget仅返回在全局环境中创建的对象/对象。 "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))

推荐阅读