R 数据帧列表
我有一个叫做“mylist”的列表。它包含两个项目。每个项目都是数据帧列表。列表的第一项是1个数据帧的列表,第二项是2个数据帧的列表,如下所示:-R 数据帧列表,r,dataframe,R,Dataframe,我有一个叫做“mylist”的列表。它包含两个项目。每个项目都是数据帧列表。列表的第一项是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
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
testvar <- mylist
列表本身看起来是这样的:-
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
testvar <- mylist
如果我将列表“mylist”分配给如下变量:-
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
testvar <- mylist
但以下命令给出了一个错误:-
str(get(paste0("testvar", "[[1]]")))
错误
为什么上面的命令找不到testvar对象,它实际上是列表“mylist”。我希望能够获得列表“mylist”第一项的结构(甚至类)。我需要以编程的方式来做,不能硬编码
有什么建议吗
致意
Deepak
get
或mget
仅返回在全局环境中创建的对象<代码>“testvar”是一个使用值创建的对象,而“testvar[[1]]”不是对象标识符,它只是列表的元素之一。因此,我们使用[
get("testvar")[[1]]
这类似于获取data.frame的列
data(mtcars)
get("mtcars") # // => works
get("mtcars[[1]]") # // => returns error
get(“mtcars[[1]]”中出错:未找到对象“mtcars[[1]]”
我们不清楚为什么需要使用get
。如果目的是在mylist
上循环,可以使用lappy
lapply(mylist, function(innerlst) yourfun(innerlst))
akrun@DeepakAgarwal它正在检查在全局环境中创建的标识符名称,即未创建的testvar[[1]]
,但您只有testvar
,这就是它没有创建的原因work@DeepakAgarwal.当你做了一个akrunarkun