简体   繁体   English

将列表列表转换为数据框但参考主列表

[英]convert list of list into data frame but referring to main list

I have a list of list that looks like that我有一个看起来像这样的列表列表

在此处输入图像描述

Within each list I have the object x , y , [[3]] , [[4]]在每个列表中,我都有 object xy[[3]][[4]]

在此处输入图像描述

In order to extract the object I applied following function为了提取 object 我应用了以下 function

library(stringi)

result <- stri_list2matrix(pred_kie_buffer[[i]], byrow = TRUE)

result <- as_tibble(result)

names(result) <- c("x", "y", "n", "instensity")

x                   y               n   instensity
<chr>  <chr>
639399.306714542    5360398.7862014 138 0.00115912489537968 
794942.331780556    5830014.5484984 347 0.0040689022530157

Is there a way in which I can obtain the origin of the main list (there ae 11 list in total) and it puts in the data frame.有没有一种方法可以获取主列表的来源(总共有 11 个列表)并将其放入数据框中。 For example例如

x                   y                n      instensity             wave
<chr>  <chr>
639399.306714542    5360398.7862014  138    0.00115912489537968     1
794942.331780556    5830014.5484984  347    0.0040689022530157      2 
831956.002790398    5965680.40059766 184    0.000256860699193104    11

Here it is a example of the structure of the list这是列表结构的示例

list1 <- list(x = 639399.306714542, y = 5360398.7862014, n = 138, intensity =   0.00115912489537968)

list2 <- list(x = 639399.306714543, y = 5360398.7862014, n = 132, intensity =   0.00115912489537968)

list3 <- list(x = 639399.306714545, y = 5360398.7862014, n = 135, intensity =   0.00115912489537968)

list4 <- list(x = 639399.306714442, y = 5360398.7862014, n = 136, intensity =   0.00115912489537968)

list5 <- list(x = 639399.306714542, y = 5360398.7862014, n = 138, intensity =   0.00115912489537968)

list6 <- list(x = 639399.306715542, y = 5360398.7862014, n = 132, intensity =   0.00115912489537968)

list7 <- list(x = 639399.306713542, y = 5360398.7862014, n = 139, intensity =   0.00115912489537968)

list8 <- list(x = 639399.303714542, y = 5360398.7862014, n = 111, intensity =   0.00115912489537968)

list9 <- list(x = 639399.306414542, y = 5360398.7862014, n = 112, intensity =   0.00115912489537968)

list10 <- list(x = 639399.305714542, y = 5360398.7862014, n = 113, intensity =  0.00115912489537968)

list11 <- list(x = 639399.306754542, y = 5360398.7862014, n = 144, intensity =  0.00115912489537968)

list <- list(list1, list2, list3, list4, list5, list6, list7, list8, list9, list10, list11)

With the edited data set, you just have to use dplyr::bind_rows with .id = "wave" :使用编辑后的数据集,您只需使用dplyr::bind_rows.id = "wave"

library(dplyr)
bind_rows(list, .id = "wave")

   wave        x        y     n intensity
   <chr>   <dbl>    <dbl> <dbl>     <dbl>
 1 1     639399. 5360399.   138   0.00116
 2 2     639399. 5360399.   132   0.00116
 3 3     639399. 5360399.   135   0.00116
 4 4     639399. 5360399.   136   0.00116
 5 5     639399. 5360399.   138   0.00116
 6 6     639399. 5360399.   132   0.00116
 7 7     639399. 5360399.   139   0.00116
 8 8     639399. 5360399.   111   0.00116
 9 9     639399. 5360399.   112   0.00116
10 10    639399. 5360399.   113   0.00116
11 11    639399. 5360399.   144   0.00116

You can use map and [ to get the first element of each sublist, then use dplyr::bind_rows with id = "wave" to merge into one dataframe, and then use set_names to change the name of the columns.您可以使用map[获取每个子列表的第一个元素,然后使用dplyr::bind_rowsid = "wave"合并到一个set_names的名称,然后使用。

map(l, `[`, 1) |>
  bind_rows(.id = "wave") |>
  set_names(c("wave", "x", "y", "n", "intensity"))

  wave      x     y     n intensity
1 1         1     2     3         4
3 3         1     2     3         4

With a minimal list that reproduces your problem:使用重现您的问题的最小列表:

l <- list(list(list(x = 1, y = 2, 3, 4),
          list(x = 3, y = 4, 5, 6)),
          list(list(x = 1, y = 2, 3, 4)))

#> str(l)
# List of 2
# $ :List of 2
# ..$ :List of 4
# .. ..$ x: num 1
# .. ..$ y: num 2
# .. ..$  : num 3
# .. ..$  : num 4
# ..$ :List of 4
# .. ..$ x: num 3
# .. ..$ y: num 4
# .. ..$  : num 5
# .. ..$  : num 6
# $ :List of 1
# ..$ :List of 4
# .. ..$ x: num 1
# .. ..$ y: num 2
# .. ..$  : num 3
# .. ..$  : num 4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM