Hello, I have a matrix mat1 of dim [1,8] and mat2 of dim[30,8], I want to replace the first row of mat2 with mat1, this is what I do: mat2[1,]<-mat1 but it transforms mat2 in a list I don't understand, I want it to stay a matrix... ----- Anna Lippel -- View this message in context: http://n4.nabble.com/Simple-question-on-replace-a-matrix-row-tp1427857p1427857.html Sent from the R help mailing list archive at Nabble.com.
On Jan 29, 2010, at 3:55 PM, anna wrote:> > Hello, I have a matrix mat1 of dim [1,8] and mat2 of dim[30,8], I > want to > replace the first row of mat2 with mat1, this is what I do: > mat2[1,]<-mat1 but it transforms mat2 in a list I don't understand, > I want > it to stay a matrix... >What makes you think mat1 was a matrix to begin with? > mat1 <- matrix(8:1, 1,8) > mat2 <-matrix(1:(8*8), 8,8) > mat2[1,] <- mat1 > mat2 # an 8 x 8 matrix We cannot tell what sort of errors you are making (since your provided no reproducible example), but you surely are making errors. David Winsemius, MD Heritage Laboratories West Hartford, CT
I convert them both before making matrix() on them. Let me try to send you more details ----- Anna Lippel -- View this message in context: http://n4.nabble.com/Simple-question-on-replace-a-matrix-row-tp1427857p1430130.html Sent from the R help mailing list archive at Nabble.com.
Here is how I get mat1: I have a matrix mainMat of 4 rows and 8 columns, I take the first row of it to build mat1. The problem might come from how I build mainMat --> each column comes from an lapply function which returns a list but I convert it to a matrix [4,1]... ----- Anna Lippel -- View this message in context: http://n4.nabble.com/Simple-question-on-replace-a-matrix-row-tp1427857p1430826.html Sent from the R help mailing list archive at Nabble.com.
On Jan 29, 2010, at 4:33 PM, anna wrote:> > Here is how I get mat1: I have a matrix mainMat of 4 rows and 8 > columns, I > take the first row of it to build mat1. The problem might come from > how I > build mainMat --> each column comes from an lapply function which > returns a > list but I convert it to a matrix [4,1]...Code... we want code. -- David Winsemius, MD Heritage Laboratories West Hartford, CT
Hi: Check the class and dimensions of your objects. If mat1 is either an eight element numeric vector or a one-row matrix, you should have no problem:> mat1 <- 1:8 # vector > mat2 <- matrix(rnorm(240), nrow =30) > class(mat1)[1] "integer"> class(mat2)[1] "matrix"> mat3 <- mat2 > mat3[1, ] <- mat1 > head(mat3, n = 3)[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1.00000000 2.0000000 3.0000000 4.0000000 5.0000000 6.0000000 [2,] 0.07651876 -0.8993153 -0.2798143 -0.1728898 0.2953603 -0.2125125 [3,] 0.12433595 0.6025504 0.1784886 0.2304723 0.8189139 1.7240880 [,7] [,8] [1,] 7.00000000 8.0000000 [2,] -0.08513052 -0.5989580 [3,] -1.78079197 -0.5740252> class(mat3)[1] "matrix" # check> mat1a <- matrix(1:8, nrow = 1) # one row matrix > class(mat1a)[1] "matrix"> dim(mat1a)[1] 1 8> mat2[1, ] <- mat1 > class(mat2)[1] "matrix"> dim(mat2)[1] 30 8> head(mat2, n = 3)[,1] [,2] [,3] [,4] [,5] [,6] [1,] 1.00000000 2.0000000 3.0000000 4.0000000 5.0000000 6.0000000 [2,] 0.07651876 -0.8993153 -0.2798143 -0.1728898 0.2953603 -0.2125125 [3,] 0.12433595 0.6025504 0.1784886 0.2304723 0.8189139 1.7240880 [,7] [,8] [1,] 7.00000000 8.0000000 [2,] -0.08513052 -0.5989580 [3,] -1.78079197 -0.5740252 # check It's entirely possible that either your mat1 is not a one-row numeric matrix or your mat2 is not a matrix. Check the class, dimensions, etc. HTH, Dennis On Fri, Jan 29, 2010 at 12:55 PM, anna <lippelanna24@hotmail.com> wrote:> > Hello, I have a matrix mat1 of dim [1,8] and mat2 of dim[30,8], I want to > replace the first row of mat2 with mat1, this is what I do: > mat2[1,]<-mat1 but it transforms mat2 in a list I don't understand, I want > it to stay a matrix... > > ----- > Anna Lippel > -- > View this message in context: > http://n4.nabble.com/Simple-question-on-replace-a-matrix-row-tp1427857p1427857.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Dennis, as soon as I do : > mat3[1, ] <- mat1 and class(mat3) I get a list and not a matrix anymore...So yes you drove me to the problem, mat1 is not one-row numeric matrix but a list. ----- Anna Lippel -- View this message in context: http://n4.nabble.com/Simple-question-on-replace-a-matrix-row-tp1427857p1440044.html Sent from the R help mailing list archive at Nabble.com.
On Jan 29, 2010, at 6:14 PM, anna wrote:> > Bill this is exactly what is happening, by using lapply I am having > a list > and not a numeric vector, I want a numeric vector, is there a way to > convert > that list to a numeric vector?You could see if substituting sapply would yield a matrix. It will if the inputs are appropriate.> > ----- > Anna Lippel > -- > View this message in context: http://n4.nabble.com/Simple-question-on-replace-a-matrix-row-tp1427857p1440742.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.David Winsemius, MD Heritage Laboratories West Hartford, CT