0

I want to store the output from many regression models including regression coefficients and information matrix from each model.

To store the results, it will be convenient if can use a data frame with two columns, one for the regression coefficients, and one for the information matrix. How can I create such a data frame?

res = data.frame(mu = I(matrix(0, m, n)), j = ???)

(It seems j should be an array in such a situation.)

  • 1
    You can't really store a `matrix` in one column of a `data.frame`. I recommend using a `list` of matrices. – nograpes May 06 '14 at 21:50
  • 1
    [This](http://stackoverflow.com/q/6143697/324364) might be helpful, or possibly harmful, depending on whether you think doing this is a good idea. – joran May 06 '14 at 21:52
  • Thanks. I think you are right. A list should work. – user3587399 May 06 '14 at 21:55

2 Answers2

1

You can do just not at the birth of the dataframe as you're trying. You can add it on later (As I show below). I've done the same thing on occasion and thus far no R gods have attempted to destroy me. Maybe not the best thing but a data.frame is a list so it can be done. Sometimes though the visual table format of the data.frame may be nicer than a list.

dat <- data.frame(coeff = 1:10)
dat$mats <- lapply(1:10, function(i) matrix(1:4, 2))

dat[1, 2]

## [[1]]
##      [,1] [,2]
## [1,]    1    3
## [2,]    2    4
Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519
  • 1
    You can do it straight through a `data.frame` call, e.g. - `data.frame(one=1:2, two=I(list(matrix(1:100,5),matrix(101:200,5))))` – thelatemail May 06 '14 at 22:26
  • @thelatemail Nice...either I forgot that or didn't know it. Either way nice approach if you don't mind risking the angerment of the R gods. – Tyler Rinker May 06 '14 at 22:28
0

Data.frames work best when you have rectangular data; specifically a collection of atomic vectors of same length. Trying to shove other data in there is not a good idea. Plus adding rows one-by-one to a data.frame is not an efficient operation. The general container for all objects in R is the list. I can hold anything you list and you can name the elements whatever you like. Not sure why you think you may need a data.frame.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Mainly for convenience. When I need to run a function many times (apply type of thing) and then organize the results, it is easier with a data.frame. – user3587399 May 06 '14 at 22:01
  • 1
    @user3587399 Well, the indexing with non-standard data.frames gets messed up quickly and you can't use things like `write.table` and then later `read.table` to get the same data.frame back. I really don't know what other conveniences you get from a data.frame in this case. But it sounds like you already agree that a list would be good for your needs. – MrFlick May 06 '14 at 22:05