Skip to content
Prev 349731 / 398513 Next

Combine list element by column name to make a dataframe

Hmm, in principle this looks like a merge() problem, if you first convert each list element to a data frame. That could be painful to get right though.

You could try something like this:
[1] "id"   "id1"  "math" "phy"  "che"  "bio"
[[1]]
  id  id1 math  phy  che  bio 
   1   10   50   60   70   80 

[[2]]
  id  id1 math  phy  che  bio 
   2   20   NA   45   NA   NA 

[[3]]
  id  id1 math  phy  che  bio 
   3   30   NA   NA   NA   75
id id1 math phy che bio
[1,]  1  10   50  60  70  80
[2,]  2  20   NA  45  NA  NA
[3,]  3  30   NA  NA  NA  75

(NB, this is a matrix, not a data frame, but so is your "df"!)

Notice that this does not do a proper merge on the id fields, i.e. if you have two different records with different grades (say one with "che" and another with "phy") on the same id, you get two records, not one. However, that might well be what you wanted.

(It is tempting to use
which does seem to work, but should probably be avoided because of the risk of destructive modification.)