Dear list! I have run into a problem that seems very simple but I can't find any solution to it (have searched the internet, help-files and "An introduction to R" etc without any luck). The problem is the following: I would like to create a data.frame with two components (columns), the first component being a matrix and the second component a vector. Whatever I have tried so far, I end up with a data.frame containing all the columns from the matrix plus the vector which is not what I am after. I have seen this kind of data.frame among R example datasets (oliveoil and yarn). I would greatly appreciate some help with this problem! Kind regards, Lina Hultin Rosenberg Karolinska Biomics Center Karolinska Institute Sweden
Have you thought of using a list?> a <- matrix(1:10, nrow=2) > b <- 1:5 > x <- list(a=a, b=b) > x$a [,1] [,2] [,3] [,4] [,5] [1,] 1 3 5 7 9 [2,] 2 4 6 8 10 $b [1] 1 2 3 4 5> x$a[,1] [,2] [,3] [,4] [,5] [1,] 1 3 5 7 9 [2,] 2 4 6 8 10> x$b[1] 1 2 3 4 5 -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Lina Hultin-Rosenberg Sent: 30 May 2007 10:26 To: r-help at stat.math.ethz.ch Subject: [R] matrix in data.frame Dear list! I have run into a problem that seems very simple but I can't find any solution to it (have searched the internet, help-files and "An introduction to R" etc without any luck). The problem is the following: I would like to create a data.frame with two components (columns), the first component being a matrix and the second component a vector. Whatever I have tried so far, I end up with a data.frame containing all the columns from the matrix plus the vector which is not what I am after. I have seen this kind of data.frame among R example datasets (oliveoil and yarn). I would greatly appreciate some help with this problem! Kind regards, Lina Hultin Rosenberg Karolinska Biomics Center Karolinska Institute Sweden ______________________________________________ R-help at stat.math.ethz.ch 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.
You need to use I() or something similar. E.g. A <- matrix(1:6, 2,3) data.frame(x=1:2, I(A)) X <- data.frame(x=1:2) X$A <- A both insert A as a single column. On Wed, 30 May 2007, Lina Hultin-Rosenberg wrote:> Dear list! > > I have run into a problem that seems very simple but I can't find any > solution to it (have searched the internet, help-files and "An introduction > to R" etc without any luck). The problem is the following: I would like to > create a data.frame with two components (columns), the first component being > a matrix and the second component a vector. Whatever I have tried so far, I > end up with a data.frame containing all the columns from the matrix plus the > vector which is not what I am after. I have seen this kind of data.frame > among R example datasets (oliveoil and yarn). > > I would greatly appreciate some help with this problem! > > Kind regards, > > Lina Hultin Rosenberg > Karolinska Biomics Center > Karolinska Institute > Sweden-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595