数据框列表清单

问题描述

我有一个名为“ 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(testvar)

正确给出以下输出。

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: "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

为什么上面的命令没有找到testvar对象,而该对象实际上是列表“ mylist”。我希望能够获得列表“ mylist”的第一项的结构(甚至是类)。我需要以编程方式进行操作,并且无法对其进行硬编码。

有什么建议吗?

最诚挚的问候

Deepak

解决方法

getmget仅返回在全局环境中创建的一个或多个对象。 "testvar"是使用值创建的对象,而“ testvar [[1]]”不是对象标识符,它只是list testvar的元素之一。因此,我们get对象标识符的值,并用list

提取[[元素
get("testvar")[[1]]

这类似于获取data.frame的列

data(mtcars)
get("mtcars") # // => works
get("mtcars[[1]]") # // => returns error

get(“ mtcars [[1]]”)错误:找不到对象'mtcars [[1]]'


不清楚为什么我们需要使用get。如果打算在mylist上循环,则可以使用lapply

lapply(mylist,function(innerlst) yourfun(innerlst))

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...