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).