I'm trying to combine a list of unequal data.frames; the obvious do.call(rbind, df.lst)
fails but the real problem is padding it out with NA
s.
df.lst <- list(A=data.frame(a=c(1,2),b=c(5,4),d=c(2,3),e=c(1,1),f=c(1,2),g=c(1,2)),
B=data.frame(a=c(1,2),b=c(3,2),d=c(2,3)),
C=data.frame(a=c(1,2),b=c(4,3),d=c(1,2),e=c(1,3))
)
I can see that I need to find the maximum number of columns in the longest data.frame; I can do this with the following code,
max(sapply(df.lst,ncol))
but after that I'm stuck. It is suggested that it is possible to index over the list and this automatically fills it with NA
s.
Once I have the padded list, I anticipate a simple do.call()
as described earlier. (I'm trying to keep the answer to base R and although there are many similar questions I can't seem to find an answer to this precise one).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…