Hallo everyone! I have a problem about creating a matrix... Suppose we have a vector y<-c(1,1,1,3,2) and a zero matrix, m ,with nrows=length(y) and ncol=4. The matrix would look like this: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 I want to change the first three rows with the vector c(1,2,3,4). I thought that with the command m[y==1,1:4]<-c(1,2,3,4) i would get 1 2 3 4 1 2 3 4 1 2 3 4 0 0 0 0 0 0 0 0 but instead i am getting 1 4 3 2 2 1 4 3 3 2 1 4 0 0 0 0 0 0 0 0 It seems it is filling the data by col instead by row. I want to use this technique in more complicated problems. So i do not want to have to work with the transpose matrix. Do you know another way to make this work. Thank you...
How about: y <- c(1,1,1,3,2) m <- matrix(0, nrow=length(y), ncol=4) m[y==1, ] <- matrix(1:4, nrow=sum(y == 1), ncol=4, byrow=TRUE) or, depending on your actual problem y <- c(1,1,1,3,2) m <- matrix(0, nrow=length(y), ncol=4) m[y == 1,] <- col(m[y == 1,]) Sarah On Mon, Jun 20, 2011 at 3:54 PM, Costis Ghionnis <conighion at gmail.com> wrote:> Hallo everyone! I have a problem about creating a matrix... > > Suppose we have a vector y<-c(1,1,1,3,2) > > and a zero matrix, m ,with nrows=length(y) and ncol=4. > > The matrix would look like this: > 0 ? ? ? 0 ? ? ? 0 ? ? ? 0 > 0 ? ? ? 0 ? ? ? 0 ? ? ? 0 > 0 ? ? ? 0 ? ? ? 0 ? ? ? 0 > 0 ? ? ? 0 ? ? ? 0 ? ? ? 0 > 0 ? ? ? 0 ? ? ? 0 ? ? ? 0 > > I want to change the first three rows with the vector c(1,2,3,4). > I thought that with the command m[y==1,1:4]<-c(1,2,3,4) i would get > > 1 ? ? ? 2 ? ? ? 3 ? ? ? 4 > 1 ? ? ? 2 ? ? ? 3 ? ? ? 4 > 1 ? ? ? 2 ? ? ? 3 ? ? ? 4 > 0 ? ? ? 0 ? ? ? 0 ? ? ? 0 > 0 ? ? ? 0 ? ? ? 0 ? ? ? 0 > > but instead i am getting > > 1 ? ? ? 4 ? ? ? 3 ? ? ? 2 > 2 ? ? ? 1 ? ? ? 4 ? ? ? 3 > 3 ? ? ? 2 ? ? ? 1 ? ? ? 4 > 0 ? ? ? 0 ? ? ? 0 ? ? ? 0 > 0 ? ? ? 0 ? ? ? 0 ? ? ? 0 > > It seems it is filling the data by col instead by row. I want to use this technique in more complicated problems. > So i do not want to have to work with the transpose matrix. Do you know another way to make this work. Thank you... >-- Sarah Goslee http://www.functionaldiversity.org
On Jun 20, 2011, at 3:54 PM, Costis Ghionnis wrote:> Hallo everyone! I have a problem about creating a matrix... > > Suppose we have a vector y<-c(1,1,1,3,2) > > and a zero matrix, m ,with nrows=length(y) and ncol=4. > > The matrix would look like this: > 0 0 0 0 > 0 0 0 0 > 0 0 0 0 > 0 0 0 0 > 0 0 0 0 > > I want to change the first three rows with the vector c(1,2,3,4). > I thought that with the command m[y==1,1:4]<-c(1,2,3,4) i would get > > 1 2 3 4 > 1 2 3 4 > 1 2 3 4 > 0 0 0 0 > 0 0 0 0Try: > m[y==1,1:4]<-rep( c(1,2,3,4), each= sum(y==1) ) > m [,1] [,2] [,3] [,4] [1,] 1 2 3 4 [2,] 1 2 3 4 [3,] 1 2 3 4 [4,] 0 0 0 0 [5,] 0 0 0 0 -- David.> > but instead i am getting > > 1 4 3 2 > 2 1 4 3 > 3 2 1 4 > 0 0 0 0 > 0 0 0 0 > > It seems it is filling the data by col instead by row. I want to use > this technique in more complicated problems. > So i do not want to have to work with the transpose matrix. Do you > know another way to make this work. Thank you... > > ______________________________________________ > 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 West Hartford, CT
#Hallo again.. Thank you for your answers. To sum up: #The problem was that we have the matrix m m<-matrix(numeric(length=5*4),nrow=5,ncol=4) m # [,1] [,2] [,3] [,4] # [1,] 0 0 0 0 # [2,] 0 0 0 0 # [3,] 0 0 0 0 # [4,] 0 0 0 0 # [5,] 0 0 0 0 #and a vector y y<-c(1,1,1,3,3) #y has informations about the rows of m, #and we wanted to change the rows that correspond to y==1 #with the vector c(1,2,3,4). The most intuitive procedure didn't work m[y==1,1:4]<-c(1,2,3,4) m # [,1] [,2] [,3] [,4] # [1,] 1 4 3 2 # [2,] 2 1 4 3 # [3,] 3 2 1 4 # [4,] 0 0 0 0 # [5,] 0 0 0 0 #because the matrix is being filled by column. The second thought was to #work with the transpose matrix. m_temp<-t(m) m_temp[1:4,y==1]<-c(1,2,3,4) m<-t(m_temp) #R assigns to an object another object of the same class. So the other way proposed #by Sara is to to assign to the submatrix m[y==1,] #of m another matrix matrix(1:4,nrow=sum(y==1),ncol=ncol(m),byrow=T) #That is: m[y==1,]<-matrix(1:4,nrow=sum(y==1),ncol(m),byrow=T) m # [,1] [,2] [,3] [,4] # [1,] 1 2 3 4 # [2,] 1 2 3 4 # [3,] 1 2 3 4 # [4,] 0 0 0 0 # [5,] 0 0 0 0 #The last way to do this was proposed by David, Patric and it is discussed #in Circle 8 (8.3.25--replacing pieces of a matrix) of R-inferno book. m[y==1,1:4]<-rep(c(1,2,3,4),each=sum(y==1))