Home > Enterprise >  convert list of list into data frame but referring to main list
convert list of list into data frame but referring to main list

Time:08-25

I have a list of list that looks like that

enter image description here

Within each list I have the object x, y, [[3]], [[4]]

enter image description here

In order to extract the object I applied following 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. 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)

CodePudding user response:

With the edited data set, you just have to use dplyr::bind_rows with .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(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
  • Related