简体   繁体   中英

How to convert a list of vectors of unequal length to a data frame

I need to convert the following list to a data frame:

list(c(13, 5, 9, 16, 1, 7, 3, 20), c(0, 1, 2, 7, 8, 14, 20), c(2, 4, 7, 9, 12, 14, 16), 0:9, c(18, 19, 20, 21, 22, 23, 6, 7, 8, 9), c(0, 1, 7, 13, 19, 6, 12, 18, 2, 8), 23:22, c(18, 13, 8), c(18, 13, 8, 3, 10, 17, 12, 6, 0, 1), c(18, 14), c(18, 19, 20, 21, 13, 7, 8, 14, 2, 1), c(13, 15, 16, 9, 8, 7, 14, 20, 19, 18))

The list has the following structure:

List of 1
 $ :List of 12
  ..$ : int [1:8] 13 5 9 16 1 7 3 20
  ..$ : int [1:7] 0 1 2 7 8 14 20
  ..$ : int [1:7] 2 4 7 9 12 14 16
  ..$ : int [1:10] 0 1 2 3 4 5 6 7 8 9
  ..$ : int [1:10] 18 19 20 21 22 23 6 7 8 9
  ..$ : int [1:10] 0 1 7 13 19 6 12 18 2 8
  ..$ : int [1:2] 23 22
  ..$ : int [1:3] 18 13 8
  ..$ : int [1:10] 18 13 8 3 10 17 12 6 0 1
  ..$ : int [1:2] 18 14
  ..$ : int [1:10] 18 19 20 21 13 7 8 14 2 1
  ..$ : int [1:10] 13 15 16 9 8 7 14 20 19 18

Because every vector could consist of 12 integers, I want to convert this list to a data frame which looks like this:

    P01 P02 P03 P04 P05 P06 P07 P08 P09 P10 P11 P12
D01 13  5   9   16  1   7   3   20  NA  NA  NA  NA
D02 0   1   2   7   8   14  20  NA  NA  NA  NA  NA
D03 2   4   7   9   12  14  16  NA  NA  NA  NA  NA

... and so on

Any tips to fix that are greatly appreciated.

Assuming your list of lists is named x , you could use

sapply(1:12, function(i) sapply(x, `[`, i))

to get the data you want. Here we iterate over the different indexes you want to extract from each vector. It just so happens in R when you ask for a position that doesn't exist, you get NA . You just need to add row/column names with whatever values you want.

You can do that by modifying the length on the original vectors in the list. Le's call the list a

a <- list(
    c(13, 5, 9, 16, 1, 7, 3, 20),
    c(0, 1, 2, 7, 8, 14, 20),
    c(2, 4, 7, 9, 12, 14, 16),
    0:9,
    c(18, 19, 20, 21, 22, 23, 6, 7, 8, 9),
    c(0, 1, 7, 13, 19, 6, 12, 18, 2, 8),
    23:22,
    c(18, 13, 8),
    c(18, 13, 8, 3, 10, 17, 12, 6, 0, 1),
    c(18, 14),
    c(18, 19, 20, 21, 13, 7, 8, 14, 2, 1),
    c(13, 15, 16, 9, 8, 7, 14, 20, 19, 18)
  )

Then compute the maximum length

n <- max(sapply(a, length))

And modify the lengths of each element in the list

b <- lapply(a, function(el) {length(el) <- n ; el})
res <- do.call("rbind", b)

Finally, change the names

dimnames(res) <- list(sprintf("D%02i", 1:nrow(res)), 
                     sprintf("P%02i", 1:ncol(res)))
res
# P01 P02 P03 P04 P05 P06 P07 P08 P09 P10
# D01  13   5   9  16   1   7   3  20  NA  NA
# D02   0   1   2   7   8  14  20  NA  NA  NA
# D03   2   4   7   9  12  14  16  NA  NA  NA
# D04   0   1   2   3   4   5   6   7   8   9
# D05  18  19  20  21  22  23   6   7   8   9
# D06   0   1   7  13  19   6  12  18   2   8
# D07  23  22  NA  NA  NA  NA  NA  NA  NA  NA
# D08  18  13   8  NA  NA  NA  NA  NA  NA  NA
# D09  18  13   8   3  10  17  12   6   0   1
# D10  18  14  NA  NA  NA  NA  NA  NA  NA  NA
# D11  18  19  20  21  13   7   8  14   2   1
# D12  13  15  16   9   8   7  14  20  19  18

Convert each component of the list L to a ts series, cbind them together, remove the messy names it assign and transpose. This will give a matrix withy one row per component and as many columns as the longest component.

t(unname(do.call("cbind", lapply(L, ts))))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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