[R] Using apply with more than one matrix

David Winsemius dwinsemius at comcast.net
Wed Apr 30 23:31:27 CEST 2014


On Apr 30, 2014, at 1:21 PM, Waichler, Scott R wrote:

> Ok, here is a toy example.  I want to use a custom function that depends on more than one matrix/array as input, and I can't figure out how to do that with apply. 
> 
> v <- c(NA, 1.5, NA, NA,
>       NA, 1.1, 0.5, NA,
>       NA, 1.3, 0.4, 0.9)
> a1 <- array(v, dim=c(2,2,3))
> m1 <- matrix(c(NA, 1.5, 2.1, NA), ncol=2, byrow=T)
> m2 <- matrix(runif(n=4, min=1, max=3), ncol=2)
> condition1 <- ifelse(!is.na(m1) & m1 > m2, T, F)

Just use:

condition1 <- !is.na(m1) & m1 > m2

For the next set of operation you should heed Jim Holtman's tagline:

"What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it."


> 
> ans <- matrix(NA, ncol=2, nrow=2) # initialize
> for(i in 1:2) {
>  for(j in 1:2) {
>    ind.not.na <- which(!is.na(a1[i,j,]))
>    if(condition1[i,j] && length(ind.not.na) > 0) {
>      ans[i,j] <- a1[i,j,ind.not.na[1]] + m2[i,j]
>    }
>  }
> }

At least in the realizatiuon I used all of the entries for conditon1 were FALSE, so a better test might be to specify set.seed() to give a reproducible example.

-- 

David.


> 
> Scott Waichler
> 
> 
> 
>> -----Original Message-----
>> From: Bert Gunter [mailto:gunter.berton at gene.com]
>> Sent: Wednesday, April 30, 2014 12:18 PM
>> To: Waichler, Scott R
>> Cc: r-help at r-project.org
>> Subject: Re: [R] Using apply with more than one matrix
>> 
>> Scott:
>> 
>> Your problem specification is rather vague: What do you mean by "use"?
>> 
>> In general, matrices are merely vectors with a dim attribute, so if you
>> can do what you want with them as vectors, then that will work for them as
>> matrices. For example:
>> 
>>> m1<- matrix(1:6, nr=3)
>>> m2 <- matrix(11:16, nr=2)
>>> m2*m2
>>     [,1] [,2] [,3]
>> [1,]  121  169  225
>> [2,]  144  196  256
>> 
>> If this does not meet your needs, you will have to follow the posting
>> guide and provide both code and a minimal reproducible example.
>> 
>> Cheers,
>> Bert
>> 
>> 
>> Bert Gunter
>> Genentech Nonclinical Biostatistics
>> (650) 467-7374
>> 
>> "Data is not information. Information is not knowledge. And knowledge is
>> certainly not wisdom."
>> H. Gilbert Welch
>> 
>> 
>> 
>> 
>> On Wed, Apr 30, 2014 at 10:54 AM, Waichler, Scott R
>> <Scott.Waichler at pnnl.gov> wrote:
>>> Hi,
>>> 
>>> I want to apply a custom function to all the elements of one matrix.
>> The function uses the value in the same i,j in a second matrix.  How can I
>> use apply() or similar function to avoid nested loops in i and j?
>>> 
>>> Thanks,
>>> Scott Waichler
>>> Pacific Northwest National Laboratory
>>> Richland, WA, USA
>>> 
>>> ______________________________________________
>>> 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