In a simple situation, converting an even list to a data frame can be done by as.data.frame()
. For example :
> (x1 <- list(a = 1:3, b = 4:6, c = 7:9))
> as.data.frame(x1)
# a b c
# 1 1 4 7
# 2 2 5 8
# 3 3 6 9
However, if the lengths of components in a list are not equal, then as.data.frame()
doesn't work.
> (x2 <- list(a = 1:4, b = 5:6, c = 7:11, d = 12:14))
# $a
# [1] 1 2 3 4
# $b
# [1] 5 6
# $c
# [1] 7 8 9 10 11
# $d
# [1] 12 13 14
> as.data.frame(x2) # get an error
I want to achieve two goals. One is :
# a b c d
# 1 1 5 7 12
# 2 2 6 8 13
# 3 3 NA 9 14
# 4 4 NA 10 NA
# 5 NA NA 11 NA
And the other is :
# V1 V2 V3 V4 V5
# a 1 2 3 4 NA
# b 5 6 NA NA NA
# c 7 8 9 10 11
# d 12 13 14 NA NA
I know there are some functions or base methods especially for these two cases. Please give me a little help.