Data.frame with column containing matrix in R

I'm trying to put some dataframe matrix of R, something like:

m <- matrix(c(1,2,3,4), nrow=2, ncol=2) df <- data.frame(id=1, mat=m) 

But when I do, I get dataframe with 2 rows and 3 columns instead dataframe with 1 row and 2 columns.

Reading the documentation, I need to hide my matrix using the I ().

 df <- data.frame(id=1, mat=I(m)) str(df) 'data.frame': 2 obs. of 2 variables: $ id : num 1 1 $ mat: AsIs [1:2, 1:2] 1 2 3 4 

As I understand it, the data frame contains one row for each row of the matrix, and the mat field contains a list of column values โ€‹โ€‹of the matrix.

So how can I get a data matrix containing matrices?

Thanks!

+4
source share
4 answers

I find data.frames, containing matrix that trouble the imagination, but the only way that I know that, hidden in the stats:::simulate.lm

Try this, slip it in and see what happens:

 d <- data.frame(y=1:5,n=5) g0 <- glm(cbind(y,ny)~1,data=d,family=binomial) debug(stats:::simulate.lm) s <- simulate(g0,n=5) , ny) ~ d <- data.frame(y=1:5,n=5) g0 <- glm(cbind(y,ny)~1,data=d,family=binomial) debug(stats:::simulate.lm) s <- simulate(g0,n=5) ) d <- data.frame(y=1:5,n=5) g0 <- glm(cbind(y,ny)~1,data=d,family=binomial) debug(stats:::simulate.lm) s <- simulate(g0,n=5) 

This is a weird back door solution. Create a list, change the class on data.frame , and then (this is required) to set names and row.names manually (if you do not do the final steps, the data will still be in place, but it will be printed as if he had zero line ...)

 m1 <- matrix(1:10,ncol=2) m2 <- matrix(5:14,ncol=2) dd <- list(m1,m2) class(dd) <- "data.frame" names(dd) <- LETTERS[1:2] row.names(dd) <- 1:5 dd ) m1 <- matrix(1:10,ncol=2) m2 <- matrix(5:14,ncol=2) dd <- list(m1,m2) class(dd) <- "data.frame" names(dd) <- LETTERS[1:2] row.names(dd) <- 1:5 dd 
+5
source

A simpler way to do this - to determine the data frame using the placeholder for the matrix

 m <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2) df <- data.frame(id = 1, mat = rep(0, nrow(m))) 

Then assign a matrix. No need to play with the class list or use the *apply() .

 df$mat <- m 
+4
source

I encountered the same problem, trying to understand the data in gasoline pls package. Use $ to set. First, let's create a matrix, called her spectra_mat, then the vector, called response_var1.

 spectra_mat = matrix(1:45, 9, 5) response_var1 = seq(1:9) 

Now we put the response_var1 vector in a new data frame - let's call it df.

 df = data.frame(response_var1) df$spectra = spectra_mat 

To check,

 str(df) 'data.frame': 9 obs. of 2 variables: $ response_var1: int 1 2 3 4 5 6 7 8 9 $ spectra : int [1:9, 1:5] 1 2 3 4 5 6 7 8 9 10 ... 
+2
source

The result you get (2 rows x 3 columns) should be expected from R, since it is equal to the cbind vector ( id , with processing) and matrix ( m ).

IMO, it would be better to use list or array (when the sizes are consistent, mixing values โ€‹โ€‹of numeric and multipliers is not allowed) if you really want to connect different data structures. Otherwise just cbind your matrix data.frame to an existing file, if both have the same number of rows that will perform the task. for instance

 x1 <- replicate(2, rnorm(10)) x2 <- replicate(2, rnorm(10)) x12l <- list(x1=x1, x2=x2) x12a <- array(rbind(x1, x2), dim=c(10,2,2)) 

and the results are read

 > str(x12l) List of 2 $ x1: num [1:10, 1:2] -0.326 0.552 -0.675 0.214 0.311 ... $ x2: num [1:10, 1:2] -0.164 0.709 -0.268 -1.464 0.744 ... > str(x12a) num [1:10, 1:2, 1:2] -0.326 0.552 -0.675 0.214 0.311 ... 

Lists are easier to use if you plan to use a matrix of different sizes and provided that they are organized the same way (for rows) as an external data.frame, you can easily multiply them. Here is an example:

 df1 <- data.frame(grp=gl(2, 5, labels=LETTERS[1:2]), age=sample(seq(25,35), 10, rep=T)) with(df1, tapply(x12l$x1[,1], list(grp, age), mean)) , df1 <- data.frame(grp=gl(2, 5, labels=LETTERS[1:2]), age=sample(seq(25,35), 10, rep=T)) with(df1, tapply(x12l$x1[,1], list(grp, age), mean)) 

You can also use the functions lapply (for a list) and apply (for an array).

+1
source

All Articles