[R] How to insert a vector or matrix into an existing matrix

David Winsemius dwinsemius at comcast.net
Sun Apr 20 22:16:11 CEST 2008


Gabor Csardi <csardi at rmki.kfki.hu> wrote in
news:20080420133926.GA28167 at localdomain: 

> Hmm, my understanding is different,
> 
> m <- matrix(sample(10*10), ncol=10)
> m2 <- rbind( m[1:5,], 1:10, m[6:10,] )
> m3 <- cbind( m[,1:8], 1:10, m[,9:10] )

I read the question the same way and, in response to the part of the 
question asking for no temporary matrix, offer this refinement on your 
suggestion:

m <- rbind(  m[1:5,], 1:10, m[6:10,] ) # row insertion or ...

# not to be followed by, but rather instead column insertion ..
m <- cbind( m[,1:8], 1:10, m[,9:10] )

-- 
David Winsemius

> 
> G.
> 
> On Sun, Apr 20, 2008 at 10:21:47AM -0300, Henrique Dallazuanna
> wrote: 
>> If I understand:
>> 
> 
>> m <- matrix(sample(10*10), ncol=10)
>> m[5:6, 8:9] <- 1:4
>> 
> 
>> 
> 
>> 
> 
>> On 4/18/08, Ng Stanley <stanleyngkl at gmail.com> wrote:
>> > Hi,
>> >
>> > Is there any functions to insert a vector or matrix into an
>> > existing ma 
> trix
>> > say between row 5 and 6 or column 8 and 9, without creating a
>> > temporary matrix ?
>> >
>> > Thanks
>> > Stanley



More information about the R-help mailing list