1

I am looking to cbind lm summary stored in different lists. I have like 20 different models stored, so I would like to avoir cbind by hand.

Do you have any idea how I could do this ?

Here are a sample of my list

data = list(structure(list(`a alone` = structure(c(4L, 5L, 1L, 2L, 3L
), .Names = c("(Intercept)", "isexMALE", "numchild_rec1", "numchild_rec2", 
"numchild_rec>2"), .Label = c("-59.819", "-61.193", "-72.312", 
"126.679", "9.825"), class = "factor"), pvalue = structure(c(2L, 
1L, 2L, 2L, 2L), .Names = c("(Intercept)", "isexMALE", "numchild_rec1", 
"numchild_rec2", "numchild_rec>2"), .Label = c(" ", "***"), class =         "factor")), .Names = c("a alone", 
"pvalue"), row.names = c("(Intercept)", "isexMALE", "numchild_rec1", 
"numchild_rec2", "numchild_rec>2"), class = "data.frame"), structure(list(
    `b partner` = structure(c(4L, 5L, 1L, 2L, 3L), .Names = c("(Intercept)", 
"isexMALE", "numchild_rec1", "numchild_rec2", "numchild_rec>2"
), .Label = c("-222.064", "-259.233", "-277.213", "365.149", 
"8.608"), class = "factor"), pvalue = structure(c(2L, 1L, 
2L, 2L, 2L), .Names = c("(Intercept)", "isexMALE", "numchild_rec1", 
"numchild_rec2", "numchild_rec>2"), .Label = c(" ", "***"
), class = "factor")), .Names = c("b partner", "pvalue"), row.names = c("(Intercept)", 
"isexMALE", "numchild_rec1", "numchild_rec2", "numchild_rec>2"
), class = "data.frame"), structure(list(`c child` = structure(c(4L, 
1L, 5L, 2L, 3L), .Names = c("(Intercept)", "isexMALE", "numchild_rec1", 
"numchild_rec2", "numchild_rec>2"), .Label = c("-36.267", "112.03", 
"119.228", "23.706", "86.989"), class = "factor"), pvalue = structure(c(1L, 
2L, 2L, 2L, 2L), .Names = c("(Intercept)", "isexMALE", "numchild_rec1", 
"numchild_rec2", "numchild_rec>2"), .Label = c("**", "***"), class =     "factor")), .Names = c("c child", 
"pvalue"), row.names = c("(Intercept)", "isexMALE", "numchild_rec1", 
 "numchild_rec2", "numchild_rec>2"), class = "data.frame"))

I would like to get an output like this

               a alone pvalue b partner pvalue c child pvalue
(Intercept)    126.679    ***   365.149    ***  23.706     **
isexMALE         9.825            8.608        -36.267    ***
numchild_rec1  -59.819    ***  -222.064    ***  86.989    ***
numchild_rec2  -61.193    ***  -259.233    ***  112.03    ***
numchild_rec>2 -72.312    ***  -277.213    *** 119.228    ***
giac
  • 4,261
  • 5
  • 30
  • 59

2 Answers2

4

Try this:

 data<- as.data.frame(data)
erasmortg
  • 3,246
  • 1
  • 17
  • 34
1

If you wanted to use cbind, you could do:

data <- do.call(cbind, data)

There is a subtle difference in the output between this solution and the accepted answer, and that is that cbind will preserve the variable names, whereas data.frame will change them (to avoid duplicated column names and also eliminate spaces). So:

data_cb <- do.call(cbind, data)
data_df <- data.frame(data)

names(data_cb)
[1] "a alone"   "pvalue"    "b partner" "pvalue"    "c child"   "pvalue" 

names(data_df)
[1] "a.alone"   "pvalue"    "b.partner" "pvalue.1"  "c.child"   "pvalue.2"

This illustrates the fact that cbind might not always produce the output in the desired format. See more discussion on this issue here and link in the respective comment.

Community
  • 1
  • 1
hugot
  • 946
  • 6
  • 8