[R] Replacing several rows of a matrix at once

2010-11-28 Thread Bryan Hanson
Hello Folks. This must be a silly question with a (not) obvious (to me) answer. Consider this: tmp - matrix(1:200, nrow = 20) vec - 300:309 tmp[9,] - vec # replacing one row works fine p - c(3, 11, 17) tmp[p,] - vec # replacing multple rows pastes the values down a column and recycles vec.

Re: [R] Replacing several rows of a matrix at once

2010-11-28 Thread Michael Sumner
vec is being recycled column wise, so you can repeat each element the required number of times: tmp[p,] - rep(vec, each = length(p)) There's many ways to achieve this though, so it depends on what other variations you might want to deal with. Cheers, Mike. On Mon, Nov 29, 2010 at 2:53 PM,

Re: [R] Replacing several rows of a matrix at once

2010-11-28 Thread Joshua Wiley
Hi Bryan, The reason vec gets recycled is that you are replacing more values than vec has. Just look at: tmp[p, ] this is 3 x 10 matrix, which you are trying to replace with a vector of length 10. If you want the replacement to occur without any recycling, you'll need to make vec be a matrix

Re: [R] Replacing several rows of a matrix at once

2010-11-28 Thread Peter Ehlers
On 2010-11-28 19:53, Bryan Hanson wrote: Hello Folks. This must be a silly question with a (not) obvious (to me) answer. Consider this: tmp- matrix(1:200, nrow = 20) vec- 300:309 tmp[9,]- vec # replacing one row works fine p- c(3, 11, 17) tmp[p,]- vec # replacing multple rows pastes the

Re: [R] Replacing several rows of a matrix at once

2010-11-28 Thread Bryan Hanson
Thanks to Michael, Josh and Jorge - Problem fixed. Michael's suggestion was what I needed, but I wouldn't have ever conceptualized it that way, and Jorge showed me how simple the function could be (at this hour, I was imagining it would be more work). Thanks guys. Bryan On 11/28/10 11:03 PM,