1

There is a tricky behaviour of making a matrix as one column of a data.frame or a S4Vector DataFrame, details is in the following, who knows the underlying reason for this? And is this consider acceptable use of the S4Vector DataFrame, or is this too much hack and shall be discouraged.

S4Vector DataFrame behaviour:

>library(S4Vectors)
>m <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2) 
>df <- DataFrame(col_1 = "abc", col_2 = rep(0, nrow(m))) 
>df$ <- m 
>df 
DataFrame with 2 rows and 2 columns 
        col_1    col_2 
    character   matrix  
1         abc      1 3 
2         abc      2 4 



Traditional data.frame behaviour:

>m  <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2) 
>df <- data.frame(col_1 = "abc", col_2 = rep(0, nrow(m))) 
>df 
 col_1 col_2 
1  abc   0 
2  abc   0 

>df$col_2 <- m 
>df 
  col_1 col_2.1 col_2.2 
1   abc       1       3 
2   abc       2       4 

Of course, here you can also choose to use the I() function to change the behaviour, which was discussed in another post as in the comments below.

Tim
  • 11
  • 1
  • This is somewhat related to the following, but extends further to S4Vectors. [click here for related post](http://stackoverflow.com/questions/13504764/r-store-a-matrix-into-a-single-dataframe-cell) – Tim Aug 01 '15 at 15:47
  • I'd suggest asking questions about S4Vectors on the Bioconductor [support forum](https://support.bioconductor.org), where the S4Vectors developers are more likely to see it; my bet is that the developers will say this is acceptable. – Martin Morgan Aug 01 '15 at 19:02

0 Answers0