问题
Hello Dear Community,
Following this post,
I am searching how to create multiple data frames from a list that contains multiple sublists with different names
So, from a list called tier.col
I would like to create a function tier2col
that returns to multiple data.frames (as much as there is sublist in my list) containing specific elements of the sublist.
I get how to do this manually as so :
phones <- data.frame(tier.col$phones$xmin[-1,3], tier.col$phones$xmax[-1,3], tier.col$phones$text[3])
But in reality "phones" changes in multiple names, so I would need a more generic approach to sublists:
x <- data.frame(tier.col$x$xmin[-1,3], tier.col$x$xmax[-1,3], tier.col$x$text[3])
The function would act as so :
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
}
I know that I can call all the xmin & xmax & text by this :
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)
But I don't know how to get pass this or how to use mapply on this kind of datastructure.
I would have like something like tier.col$c(names(tier.col)$xmin
to work, but evidently, that is not the proper way to do it.
Can someone help me to compute this in an efficient way?
Stucture of 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 ...
Don't hesitate to tell if something is not clear/missing,
Thanks a lot,
回答1:
If you know how to process one item (phones
), then just generalize that into a function so you can run it to the whole list via 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)
At this point, out
is a list of data.frames. It is recommended you keep it that way, but if what you really wanted are data.frames to be added to an environment (e.g. the global environment) then you can do so with list2env
:
list2env(out, envir = .GlobalEnv)
来源:https://stackoverflow.com/questions/22067166/extract-elements-of-multiple-sublists-in-a-list-and-transform-to-multiple-datafr