[R] when to use & pros/cons of dataframe vs. matrix?

Rui Barradas ruipbarradas at sapo.pt
Thu Jun 27 22:03:35 CEST 2013


Arun's answer shows that matrices are faster. If your data is all of the 
same type, then this might be a point for matrices.
data.frames are better for modeling. You can use the formula interface 
to the many modeling functions. For instance, the example below is _not_ 
possible with a matrix.

dat <- data.frame(x = rnorm(100), A = sample(letters[1:4], 100, TRUE), y 
= rnorm(100))

model <- lm(y ~ x + A, data = dat) # not possible with matrix

#predict.lm needs data.frames
newdat <- data.frame(x = c(1,3,4), A = rep("a",3))
predict(model, new = newdat)

There are many other examples like this one. If you are doing data 
modeling, use data frames.

Hope this helps,

Rui Barradas

Em 27-06-2013 19:26, Anika Masters escreveu:
> When "should" I use a dataframe vs. a matrix?  What are the pros and cons?
> If I have data of all the same type, am I usually better off using a
> matrix and not a dataframe?
> What are the advantages if any of using a dataframe vs. a matrix?
> (rownames and column names perhaps?)
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list