Transpose turns a list-of-lists "inside-out"; it turns a pair of lists into a list of pairs, or a list of pairs into pair of lists. For example, if you had a list of length n where each component had values a and b, transpose() would make a list with elements a and b that contained lists of length n. It's called transpose because x[[1]][[2]] is equivalent to transpose(x)[[2]][[1]].

transpose(.l, .names = NULL)

Arguments

.l

A list of vectors to transpose. The first element is used as the template; you'll get a warning if a subsequent element has a different length.

.names

For efficiency, transpose() bases the return structure on the first component of .l by default. Specify .names to override this.

Value

A list with indexing transposed compared to .l.

Details

Note that transpose() is its own inverse, much like the transpose operation on a matrix. You can get back the original input by transposing it twice.

Examples

x <- rerun(5, x = runif(1), y = runif(5)) x %>% str()
#> List of 5 #> $ :List of 2 #> ..$ x: num 0.589 #> ..$ y: num [1:5] 0.704 0.93 0.98 0.415 0.563 #> $ :List of 2 #> ..$ x: num 0.366 #> ..$ y: num [1:5] 0.422 0.293 0.857 0.881 0.234 #> $ :List of 2 #> ..$ x: num 0.103 #> ..$ y: num [1:5] 0.289 0.403 0.786 0.744 0.5 #> $ :List of 2 #> ..$ x: num 0.573 #> ..$ y: num [1:5] 0.3783 0.0627 0.3559 0.5603 0.1505 #> $ :List of 2 #> ..$ x: num 0.11 #> ..$ y: num [1:5] 0.464 0.435 0.236 0.505 0.699
x %>% transpose() %>% str()
#> List of 2 #> $ x:List of 5 #> ..$ : num 0.589 #> ..$ : num 0.366 #> ..$ : num 0.103 #> ..$ : num 0.573 #> ..$ : num 0.11 #> $ y:List of 5 #> ..$ : num [1:5] 0.704 0.93 0.98 0.415 0.563 #> ..$ : num [1:5] 0.422 0.293 0.857 0.881 0.234 #> ..$ : num [1:5] 0.289 0.403 0.786 0.744 0.5 #> ..$ : num [1:5] 0.3783 0.0627 0.3559 0.5603 0.1505 #> ..$ : num [1:5] 0.464 0.435 0.236 0.505 0.699
# Back to where we started x %>% transpose() %>% transpose() %>% str()
#> List of 5 #> $ :List of 2 #> ..$ x: num 0.589 #> ..$ y: num [1:5] 0.704 0.93 0.98 0.415 0.563 #> $ :List of 2 #> ..$ x: num 0.366 #> ..$ y: num [1:5] 0.422 0.293 0.857 0.881 0.234 #> $ :List of 2 #> ..$ x: num 0.103 #> ..$ y: num [1:5] 0.289 0.403 0.786 0.744 0.5 #> $ :List of 2 #> ..$ x: num 0.573 #> ..$ y: num [1:5] 0.3783 0.0627 0.3559 0.5603 0.1505 #> $ :List of 2 #> ..$ x: num 0.11 #> ..$ y: num [1:5] 0.464 0.435 0.236 0.505 0.699
# transpose() is useful in conjunction with safely() & quietly() x <- list("a", 1, 2) y <- x %>% map(safely(log)) y %>% str()
#> List of 3 #> $ :List of 2 #> ..$ result: NULL #> ..$ error :List of 2 #> .. ..$ message: chr "non-numeric argument to mathematical function" #> .. ..$ call : language .Primitive("log")(x, base) #> .. ..- attr(*, "class")= chr [1:3] "simpleError" "error" "condition" #> $ :List of 2 #> ..$ result: num 0 #> ..$ error : NULL #> $ :List of 2 #> ..$ result: num 0.693 #> ..$ error : NULL
y %>% transpose() %>% str()
#> List of 2 #> $ result:List of 3 #> ..$ : NULL #> ..$ : num 0 #> ..$ : num 0.693 #> $ error :List of 3 #> ..$ :List of 2 #> .. ..$ message: chr "non-numeric argument to mathematical function" #> .. ..$ call : language .Primitive("log")(x, base) #> .. ..- attr(*, "class")= chr [1:3] "simpleError" "error" "condition" #> ..$ : NULL #> ..$ : NULL
# Use simplify_all() to reduce to atomic vectors where possible x <- list(list(a = 1, b = 2), list(a = 3, b = 4), list(a = 5, b = 6)) x %>% transpose()
#> $a #> $a[[1]] #> [1] 1 #> #> $a[[2]] #> [1] 3 #> #> $a[[3]] #> [1] 5 #> #> #> $b #> $b[[1]] #> [1] 2 #> #> $b[[2]] #> [1] 4 #> #> $b[[3]] #> [1] 6 #> #>
x %>% transpose() %>% simplify_all()
#> $a #> [1] 1 3 5 #> #> $b #> [1] 2 4 6 #>
# Provide explicit component names to prevent loss of those that don't # appear in first component ll <- list( list(x = 1, y = "one"), list(z = "deux", x = 2) ) ll %>% transpose()
#> $x #> $x[[1]] #> [1] 1 #> #> $x[[2]] #> [1] 2 #> #> #> $y #> $y[[1]] #> [1] "one" #> #> $y[[2]] #> NULL #> #>
nms <- ll %>% map(names) %>% reduce(union) ll %>% transpose(.names = nms)
#> $x #> $x[[1]] #> [1] 1 #> #> $x[[2]] #> [1] 2 #> #> #> $y #> $y[[1]] #> [1] "one" #> #> $y[[2]] #> NULL #> #> #> $z #> $z[[1]] #> NULL #> #> $z[[2]] #> [1] "deux" #> #>