[R] Working with Matrix
David Winsemius
dwinsemius at comcast.net
Sat Jan 5 02:39:38 CET 2013
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 believe there must be some smarter method using
> ***mapply()*** or something like that. To me above operation looks
> very trivial! Also underlying function may be more complected
>
> Is it real trivial or some better method is available?
Perhaps:
> colSums(Mat[ 1:4, ] == Mat[5:8, ]) == 4
[1] TRUE TRUE TRUE TRUE FALSE
--
David.
>
> Thanks and regards,
>
> On Fri, Jan 4, 2013 at 9:24 PM, Suzen, Mehmet <msuzen at gmail.com> wrote:
>> 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 14:08, Rui Barradas <ruipbarradas at sapo.pt> 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) < nrow(y)){
>>> nas <- matrix(NA, ncol = ncol(x), nrow = nrow(y) - nrow(x))
>>> x <- rbind(x, nas)
>>> }else{
>>> nas <- matrix(NA, ncol = ncol(y), nrow = nrow(x) - nrow(y))
>>> y <- rbind(y, nas)
>>> }
>>> FUN(x, y)
>>> }
>>>
>>> fun(Mat1, Mat2)
>>> fun(Mat1, Mat2, `*`) # Note the back quotes
>>>
>>>
>>> Hope this helps,
>>>
>>> Rui Barradas
>>> Em 04-01-2013 12:06, Jorge I Velez escreveu:
>>>
>>>> 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, ncol = ncol(m1), nrow = nrow(m1) - nrow(m2))
>>>> m2 <- rbind(m2, nas)
>>>> }
>>>> list(m1, m2)
>>>> }
>>>>
>>>> # process
>>>> r <- foo(Mat1, Mat2)
>>>>
>>>> # basic operations
>>>> Reduce("+", r)
>>>> Reduce("*", r)
>>>> Reduce("/", r)
>>>> Reduce("-", r)
>>>>
>>>> HTH,
>>>> Jorge.-
>>>>
>>>>
>>>> On Fri, Jan 4, 2013 at 10:47 PM, 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 corresponding columns
>>>>> of these 2 matrices and add the corresponding elements (ignoring NA
>>>>> values if any). Therefore for the 1st column I need to do:
>>>>>
>>>>> (1+1), (2+2),...,(4+4), (NA+5)
>>>>>
>>>>> and so on
>>>>>
>>>>> And the resulting numbers will be stored in some other matrix
>>>>>
>>>>> Also note that, here I gave the example of addition, however, this can
>>>>> be any user defined function.
>>>>>
>>>>> Is there any R way to do that, without using any for-loop?
>>>>>
>>>>> Thanks and regards,
>>>>>
>>>>> ______________________________________________
>>>>> 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.
>>>>>
>>>> [[alternative HTML version deleted]]
>>>>
>>>> ______________________________________________
>>>> 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.
>>>
>>>
>>> ______________________________________________
>>> 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.
>>
>> ______________________________________________
>> 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.
>
> ______________________________________________
> 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
Alameda, CA, USA
More information about the R-help
mailing list