Re: [R] Working with Matrix

2013-01-04 Thread David Winsemius
On Jan 4, 2013, at 10:46 AM, Christofer Bogaso wrote: > Thanks all for your help. However I was looking for following type of > operation: > > Mat1 <- Mat2 <- matrix(1:20, 4, 5); Mat2[1,5] <- 200 > Mat = rbind(Mat1, Mat2) > apply(Mat, 2, function(x) return(all(x[1:4] == x[5:8]))) > > However I

Re: [R] Working with Matrix

2013-01-04 Thread Christofer Bogaso
Thanks all for your help. However I was looking for following type of operation: Mat1 <- Mat2 <- matrix(1:20, 4, 5); Mat2[1,5] <- 200 Mat = rbind(Mat1, Mat2) apply(Mat, 2, function(x) return(all(x[1:4] == x[5:8]))) However I believe there must be some smarter method using ***mapply()*** or someth

Re: [R] Working with Matrix

2013-01-04 Thread Suzen, Mehmet
Hello Rui/Jorge, This is shorter, and probably needs less memory for large matrices as you create an other copy by defining nas: matrixOp <- function(m1, m2, op=`+`) { rows <- min(nrow(m1), nrow(m2)) cols <- ncol(m1) op(m1[1:rows, 1:cols], m2[1:rows, 1:cols]) } Best, mem On 4 January 2013 1

Re: [R] Working with Matrix

2013-01-04 Thread Jorge I Velez
Muito obrigado, Rui. Cleaner and simpler than my approach. Regards, Jorge.- On Sat, Jan 5, 2013 at 12:08 AM, Rui Barradas <> wrote: > Hello, > > Using part of your code, it's possible to do without Reduce, have foo > (fun, below) do the job. > > fun <- function(x, y, FUN = `+`){ > if(nrow(x)

Re: [R] Working with Matrix

2013-01-04 Thread Rui Barradas
Hello, Using part of your code, it's possible to do without Reduce, have foo (fun, below) do the job. fun <- function(x, y, FUN = `+`){ if(nrow(x) < nrow(y)){ nas <- matrix(NA, ncol = ncol(x), nrow = nrow(y) - nrow(x)) x <- rbind(x, nas) }else{ nas <- matrix(NA,

Re: [R] Working with Matrix

2013-01-04 Thread Neal H. Walfield
At Fri, 4 Jan 2013 17:17:40 +0530, Christofer Bogaso wrote: > > Hello again, > > Let say I have 2 matrices which equal number of columns but different > number of rows like: > > Mat1 <- matrix(1:20, 4, 5) > Mat2 <- matrix(1:25, 5, 5) > > Now for each column 1-to-5 I need to fetch the correspond

Re: [R] Working with Matrix

2013-01-04 Thread Jorge I Velez
Dear Christofer, You can try the following: # proccess the matrices foo <- function(m1, m2){ if(ncol(m1) != ncol(m2)) stop('number of columns should be equal') if(nrow(m1) < nrow(m2)){ nas <- matrix(NA, ncol = ncol(m1), nrow = nrow(m2) - nrow(m1)) m1 <- rbind(m1, nas) } else{ nas <- matrix(NA

[R] Working with Matrix

2013-01-04 Thread Christofer Bogaso
Hello again, Let say I have 2 matrices which equal number of columns but different number of rows like: Mat1 <- matrix(1:20, 4, 5) Mat2 <- matrix(1:25, 5, 5) Now for each column 1-to-5 I need to fetch the corresponding columns of these 2 matrices and add the corresponding elements (ignoring NA v