提取列表中多个子列表的元素并转换为多个数据帧

时间:2014-02-27 11:30:23

标签: r list dataframe sublist

Hello亲爱的社区,

关注this post

我正在搜索如何从包含多个名称不同的子列表的列表创建多个数据框

因此,从名为tier.col的列表中,我想创建一个函数tier2col,它返回多个data.frames(与我的列表中的子列表一样多),包含子列表的特定元素

我得到如何手动执行此操作:

phones <- data.frame(tier.col$phones$xmin[-1,3], tier.col$phones$xmax[-1,3], tier.col$phones$text[3])

但实际上“手机”会改变多个名字,所以我需要一种更通用的方法来制作子列表:

x <- data.frame(tier.col$x$xmin[-1,3], tier.col$x$xmax[-1,3], tier.col$x$text[3])

该功能将如此:

tier2col <- function(list.x) {
  for all sublist in my list 
  select tier.col$sublist$xmin[-1,3], tier.col$sublist$xmax[-1,3], tier.col$sublist$text[3]
  and compile them in a dataframe
}

我知道我可以拨打所有xmin&amp; xmax&amp;文字由: lapply(tier.col[c(1:length(names(tier.col)))], "[", 1) lapply(tier.col[c(1:length(names(tier.col)))], "[", 2) lapply(tier.col[c(1:length(names(tier.col)))], "[", 3)

但我不知道如何通过这种或如何在这种数据结构上使用mapply。

我希望像tier.col$c(names(tier.col)$xmin这样的东西可以工作,但显然,这不是正确的方法。

有人可以帮助我以有效的方式计算出来吗?

tier.col的结构:

List of 17
 $ phones        :List of 3
  ..$ xmin:'data.frame':    2506 obs. of  3 variables:
  .. ..$ num.row: num [1:2506] 11 15 19 23 27 31 35 39 43 47 ...
  .. ..$ object : chr [1:2506] "xmin" "xmin" "xmin" "xmin" ...
  .. ..$ value  : Factor w/ 4466 levels "","  ","  n ",..: 20 20 21 22 23 24 26 27 28 29 ...
  ..$ xmax:'data.frame':    2506 obs. of  3 variables:
  .. ..$ num.row: num [1:2506] 12 16 20 24 28 32 36 40 44 48 ...
  .. ..$ object : chr [1:2506] "xmax" "xmax" "xmax" "xmax" ...
  .. ..$ value  : Factor w/ 4466 levels "","  ","  n ",..: 2414 21 22 23 24 26 27 28 29 30 ...
  ..$ text:'data.frame':    2505 obs. of  3 variables:
  .. ..$ num.row: num [1:2505] 17 21 25 29 33 37 41 45 49 53 ...
  .. ..$ object : chr [1:2505] "text" "text" "text" "text" ...
  .. ..$ value  : Factor w/ 4466 levels "","  ","  n ",..: 11 4397 4434 3697 4025 3697 3832 4127 3992 4397 ...
 $ syll          :List of 3
  ..$ xmin:'data.frame':    1147 obs. of  3 variables:
  .. ..$ num.row: num [1:1147] 10037 10041 10045 10049 10053 ...
  .. ..$ object : chr [1:1147] "xmin" "xmin" "xmin" "xmin" ...
  .. ..$ value  : Factor w/ 4466 levels "","  ","  n ",..: 20 20 21 24 27 30 33 35 37 40 ...
  ..$ xmax:'data.frame':    1147 obs. of  3 variables:
  .. ..$ num.row: num [1:1147] 10038 10042 10046 10050 10054 ...
  .. ..$ object : chr [1:1147] "xmax" "xmax" "xmax" "xmax" ...
  .. ..$ value  : Factor w/ 4466 levels "","  ","  n ",..: 2414 21 24 27 30 33 35 37 40 43 ...
  ..$ text:'data.frame':    1146 obs. of  3 variables:
  .. ..$ num.row: num [1:1146] 10043 10047 10051 10055 10059 ...
  .. ..$ object : chr [1:1146] "text" "text" "text" "text" ...
  .. ..$ value  : Factor w/ 4466 levels "","  ","  n ",..: 11 4431 4028 3879 4430 4442 4405 3783 4159 4327 ...
 $ delivery      :List of 3
  ..$ xmin:'data.frame':    1147 obs. of  3 variables:
  .. ..$ num.row: num [1:1147] 14627 14631 14635 14639 14643 ...
  .. ..$ object : chr [1:1147] "xmin" "xmin" "xmin" "xmin" ...
  .. ..$ value  : Factor w/ 4466 levels "","  ","  n ",..: 20 20 21 24 27 30 33 35 37 40 ...
  ..$ xmax:'data.frame':    1147 obs. of  3 variables:
  .. ..$ num.row: num [1:1147] 14628 14632 14636 14640 14644 ...
  .. ..$ object : chr [1:1147] "xmax" "xmax" "xmax" "xmax" ...
  .. ..$ value  : Factor w/ 4466 levels "","  ","  n ",..: 2414 21 24 27 30 33 35 37 40 43 ...
  ..$ text:'data.frame':    1146 obs. of  3 variables:
  .. ..$ num.row: num [1:1146] 14633 14637 14641 14645 14649 ...
  .. ..$ object : chr [1:1146] "text" "text" "text" "text" ...
  .. ..$ value  : Factor w/ 4466 levels "","  ","  n ",..: 11 2 2 2 2 2 2 2 2 2 ...
 $ link          :List of 3
  ..$ xmin:'data.frame':    807 obs. of  3 variables:
  .. ..$ num.row: num [1:807] 19217 19221 19225 19229 19233 ...
  .. ..$ object : chr [1:807] "xmin" "xmin" "xmin" "xmin" ...
  .. ..$ value  : Factor w/ 4466 levels "","  ","  n ",..: 20 20 21 27 30 34 37 40 45 1642 ...
  ..$ xmax:'data.frame':    807 obs. of  3 variables:
  .. ..$ num.row: num [1:807] 19218 19222 19226 19230 19234 ...
  .. ..$ object : chr [1:807] "xmax" "xmax" "xmax" "xmax" ...
  .. ..$ value  : Factor w/ 4466 levels "","  ","  n ",..: 2414 21 27 30 34 37 40 45 1642 1651 ...
  ..$ text:'data.frame':    806 obs. of  3 variables:
  .. ..$ num.row: num [1:806] 19223 19227 19231 19235 19239 ...
  .. ..$ object : chr [1:806] "text" "text" "text" "text" ...
  .. ..$ value  : Factor w/ 4466 levels "","  ","  n ",..: 11 2 2 4025 2 2 2 4025 2 4025 ...
 $ words         :List of 3
  ..$ xmin:'data.frame':    807 obs. of  3 variables:
  .. ..$ num.row: num [1:807] 22447 22451 22455 22459 22463 ...
  .. ..$ object : chr [1:807] "xmin" "xmin" "xmin" "xmin" ...
  .. ..$ value  : Factor w/ 4466 levels "","  ","  n ",..: 20 20 21 27 30 34 37 40 45 1642 ...
  ..$ xmax:'data.frame':    807 obs. of  3 variables:
  .. ..$ num.row: num [1:807] 22448 22452 22456 22460 22464 ...
  .. ..$ object : chr [1:807] "xmax" "xmax" "xmax" "xmax" ...
  .. ..$ value  : Factor w/ 4466 levels "","  ","  n ",..: 2414 21 27 30 34 37 40 45 1642 1651 ...
  ..$ text:'data.frame':    806 obs. of  3 variables:
  .. ..$ num.row: num [1:806] 22453 22457 22461 22465 22469 ...
  .. ..$ object : chr [1:806] "text" "text" "text" "text" ...
  .. ..$ value  : Factor w/ 4466 levels "","  ","  n ",..: 11 4424 3881 4429 3745 3781 4160 4054 3983 4229 ...

不要犹豫,判断某件事是否不清楚,

非常感谢,

1 个答案:

答案 0 :(得分:5)

如果您知道如何处理一个项目(phones),那么只需将其概括为一个函数,这样您就可以通过lapply将其运行到整个列表:

process.one <- function(x)
    data.frame(xmin = x$xmin[-1,3], xmax = x$xmax[-1,3], text = x$text[3])

out <- lapply(tier.col, process.one)

此时,out是data.frames的列表。建议您保持这种方式,但如果您真正想要的是将data.frames添加到环境(例如全局环境),那么您可以使用list2env

list2env(out, envir = .GlobalEnv)