How can I rbind vectors matching their column names?

后端 未结 7 2019
醉话见心
醉话见心 2020-12-01 16:04

rbind does not check for column names when binding together vectors:

l = list(row1 = c(10, 20), row2 = c(20, 10))
names(l$row1) = c(\"A\", \"B\")
names(l$row         


        
7条回答
  •  [愿得一人]
    2020-12-01 16:24

    rbind will work if you first change each element of l to a data frame:

    do.call("rbind", lapply(l, function(x) data.frame(as.list(x))))
    
          A  B
    row1 10 20
    row2 10 20
    

提交回复
热议问题