简体   繁体   中英

R: reorganizing sublists

mylist <- list(result.1 = list(c(3L, 3L, 3L, 3L, 3L, 3L, 7L, 3L, 3L, 3L, 
                       3L, 10L, 3L, 3L, 3L, 3L, 10L, 6L, 4L, 6L, 4L, 10L, 4L, 6L, 6L, 
                       6L, 10L, 8L, 6L, 4L, 8L, 10L, 3L, 4L, 3L, 10L, 6L, 4L, 6L, 6L, 
                       4L, 10L, 2L, 4L, 2L, 4L, 2L, 8L, 8L, 4L), 
                     c(3L, 3L, 4L, 4L, 3L, 
                                                                   4L, 10L, 3L, 10L, 4L, 4L, 10L, 10L, 10L, 10L, 4L, 10L, 10L, 4L, 
                                                                   10L, 10L, 4L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 1L, 4L, 3L, 4L, 1L, 
                                                                   3L, 3L, 10L, 4L, 1L, 4L, 4L, 10L, 4L, 4L, 4L, 4L, 8L, 8L, 4L, 
                                                                   8L), 
                     c(2L, 2L, 2L, 7L, 4L, 2L, 7L, 2L, 2L, 4L, 4L, 2L, 2L, 2L, 
                                                                          2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 6L, 6L, 1L, 4L, 1L, 
                                                                          6L, 1L, 4L, 6L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
                                                                          6L, 6L, 4L, 6L)),

     result.2 = list(c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
                       4L, 7L, 7L, 7L, 7L, 7L, 4L, 4L, 5L, 7L, 5L, 7L, 4L, 5L, 7L, 
                       7L, 7L, 7L, 7L, 7L, 4L, 7L, 7L, 4L, 7L, 4L, 4L, 3L, 4L, 4L, 
                       10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L), c(6L, 
                                                                            6L, 6L, 1L, 6L, 1L, 6L, 1L, 6L, 1L, 7L, 7L, 7L, 5L, 7L, 5L, 
                                                                            5L, 5L, 7L, 7L, 8L, 7L, 4L, 7L, 7L, 7L, 7L, 6L, 6L, 6L, 6L, 
                                                                            6L, 6L, 4L, 6L, 4L, 6L, 6L, 4L, 6L, 4L, 7L, 7L, 6L, 7L, 6L, 
                                                                            6L, 10L, 6L, 8L), c(1L, 4L, 1L, 6L, 9L, 6L, 1L, 6L, 1L, 6L, 
                                                                                                6L, 6L, 6L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 5L, 6L, 10L, 1L, 
                                                                                                10L, 10L, 1L, 7L, 6L, 6L, 4L, 7L, 10L, 1L, 10L, 1L, 10L, 
                                                                                                1L, 10L, 4L, 10L, 7L, 10L, 10L, 4L, 4L, 6L, 4L, 4L, 6L)))

I have a list called mylist that looks like the following.

> mylist
$result.1
$result.1[[1]]
 [1]  3  3  3  3  3  3  7  3  3  3  3 10  3  3  3  3 10  6  4  6  4 10  4  6  6  6 10  8  6  4  8
[32] 10  3  4  3 10  6  4  6  6  4 10  2  4  2  4  2  8  8  4

$result.1[[2]]
 [1]  3  3  4  4  3  4 10  3 10  4  4 10 10 10 10  4 10 10  4 10 10  4  4  4  4  4  3  4  4  1  4
[32]  3  4  1  3  3 10  4  1  4  4 10  4  4  4  4  8  8  4  8

$result.1[[3]]
 [1] 2 2 2 7 4 2 7 2 2 4 4 2 2 2 2 4 4 4 4 4 4 4 4 4 4 6 6 1 4 1 6 1 4 6 4 4 4 4 4 4 4 4 4 4 4 4 6
[48] 6 4 6


$result.2
$result.2[[1]]
 [1]  4  4  4  4  4  4  4  4  4  4  4  7  7  7  7  7  4  4  5  7  5  7  4  5  7  7  7  7  7  7  4
[32]  7  7  4  7  4  4  3  4  4 10 10 10 10 10 10 10 10 10 10

$result.2[[2]]
 [1]  6  6  6  1  6  1  6  1  6  1  7  7  7  5  7  5  5  5  7  7  8  7  4  7  7  7  7  6  6  6  6
[32]  6  6  4  6  4  6  6  4  6  4  7  7  6  7  6  6 10  6  8

$result.2[[3]]
 [1]  1  4  1  6  9  6  1  6  1  6  6  6  6  5  5  5  5  5  6  6  5  6 10  1 10 10  1  7  6  6  4
[32]  7 10  1 10  1 10  1 10  4 10  7 10 10  4  4  6  4  4  6

It contains two result s. Within each result , I have 3 sublists. I want to reorganize them in the following way:

> new_mylist
[[1]]
result.1 3  3  3  3  3  3  7  3  3  3  3 10 ...
result.2 4  4  4  4  4  4  4  4  4  4  4  7 ...

[[2]]
result.1 3  3  4  4  3  4 10  3 10  4  4 10 ...
result.2 6  6  6  1  6  1  6  1  6  1  7  7 ...

[[3]]
result.1 2 2 2 7 4 2 7 2 2 4 4 2 2 2 2 4 4 ...
result.2 1  4  1  6  9  6  1  6  1  6  6  6  ...

I would like new_mylist to contain 3 sublists. Within each sublist, I'm storing the vector from result.1 and result.2 . Is there a quick and generalizing way to reorganize lists this way?

The easiest would be transpose from purrr

library(purrr)
library(dplyr)
mylist %>%
   transpose

If we need a tibble or data.frame

mylist %>% 
   transpose %>% 
   map(as_tibble)

You can use mapply in base R:

mapply(list, mylist$result.1, mylist$result.2, SIMPLIFY = FALSE)

Or more generically (if you have more than 2 results in the list or with arbitrary names for example):

do.call(function(...) mapply(list, ..., SIMPLIFY = FALSE), mylist)

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