I have a list:
ls <- list(c(\"a\", \"b\", \"c\"), c(\"1\", \"2\", \"3\"), c(\"foo\", \"bar\", \"baz\"))
ls
#> [[1]]
#> [1] \"a\" \"b\" \"c\"
#>
In the data.table
package, there's a transpose()
function which does exactly this. It is implemented in C
for speed.
require(data.table) # v1.9.6+
transpose(ls)
# [[1]]
# [1] "a" "1" "foo"
# [[2]]
# [1] "b" "2" "bar"
# [[3]]
# [1] "c" "3" "baz"
It also fills automatically with NA
in case the list elements are not of equal lengths, and also coerces automatically to the highest SEXPTYPE. You can provide a different value to the fill
argument if necessary. Check ?transpose
.