[R] Filling matrix elements with a function

arun smartpink111 at yahoo.com
Tue Nov 6 15:03:40 CET 2012


Hi,
I guess this should also work.
f.1 <- function(x) x * x
list1<-lapply(rep(list(f.1),25),function(x) x)
mat1<-array(unlist(list1),dim=c(5,5))
 mat1[[1,1]](3)
#[1] 9

A.K.




----- Original Message -----
From: jim holtman <jholtman at gmail.com>
To: Jessica Streicher <j.streicher at micromata.de>
Cc: r-help <r-help at r-project.org>; Aimee Kopolow <alj27 at georgetown.edu>
Sent: Tuesday, November 6, 2012 8:10 AM
Subject: Re: [R] Filling matrix elements with a function

Try this if you want a matrix of functions:


> # define matrix of list()
> x <- vector('list', 25)
> dim(x) <- c(5,5)
> f.1 <- function(x) x * x
> x[[1,1]] <- f.1
> x[[2,2]] <- f.1
> # call the functions
> x[[2,2]](42)
[1] 1764
> x[[1,1]](3)
[1] 9
>



On Tue, Nov 6, 2012 at 6:01 AM, Jessica Streicher
<j.streicher at micromata.de> wrote:
> Not that i really understand what that shall do, but..
>
> why do you define 2 functions that do exactly the same?
> why do you want to make a matrix of functions instead of a matrix with the results of functions?
>
> On 06.11.2012, at 05:11, Aimee Kopolow wrote:
>
>> Hi all,
>>
>>
>> I have a matrix simulating migration in a spatial model. I want to be
>> able to define movement (the values of m1, m2 and m3) as only != 0
>> between adjacent patches and contingent on certain conditions as
>> defined in the function.
>>
>> Here is the code:
>>
>> WET<-function(t) {everglades$precipitation[t]}    #simply reads
>> precipitation data from a csv, value is numeric
>>
>> AB<-function(WET,t) {ifelse(WET(t)>0, 0.0001, 0)}
>> BC<-function(WET,t) {ifelse(WET(t)>0, 0.0001, 0)}
>>
>> NP<-4
>> m1 <- matrix(0, NP,NP)
>>  m2 <- matrix(0,NP,NP)
>>  m3 <- matrix(0,NP,NP)
>>
>>
>> for (i in 1:NP)
>>  {
>>   for (j in 1:NP)
>>   {
>>    if ((i%%sqrt(NP)) == 0 & i!=j)
>>    {
>>     if(j == i+sqrt(NP) | j == i-1 | j == i-sqrt(NP))
>>     {
>>      m1[j,i] <- AB
>>      m1[i,j] <- AB
>>
>>
>>      m2[j,i] <- 0.00025
>>      m2[i,j] <- 0.00025
>>
>>      m3[j,i] <- BC
>>
>>      m3[i,j] <- BC
>>
>>     }
>>    }
>>    if (i%%sqrt(NP) == 1)
>>    {
>>     if(j == i+sqrt(NP) | j == i+1 | j == i-sqrt(NP) )
>>     {
>>      m1[j,i] <- AB
>>
>>      m1[i,j] <- AB
>>
>>
>>      m2[j,i] <- 0.00025
>>      m2[i,j] <- 0.00025
>>
>>      m3[j,i] <- BC
>>
>>      m3[i,j] <- BC
>>     }
>>    }
>>
>>    if (i%%sqrt(NP)!=0 & i%%sqrt(NP)!=1 & i!=j)
>>    {
>>     if(j == i+sqrt(NP) | j == i+1 | j == i-sqrt(NP) | j == i-1)
>>     {
>>      m1[j,i] <- AB
>>
>>      m1[i,j] <- AB
>>
>>      m2[j,i] <- 0.00025
>>      m2[i,j] <- 0.00025
>>
>>      m3[j,i] <- BC
>>
>>      m3[i,j] <- BC
>>      }
>>    }
>>   }
>>  }
>>
>> Please could somebody advise me as to where I'm going wrong and how to
>> get it right? I have tried several different permutations and can't
>> seem to do so. I tried googling keywords "inserting function as matrix
>> element" but couldn't find any advice online. I'm very inexperienced
>> at coding so apologies if this is very simple.
>>
>> Thank you for any assistance you are able to give,
>> Aimee.
>>
>> ______________________________________________
>> 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.



-- 
Jim Holtman
Data Munger Guru

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

______________________________________________
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.





More information about the R-help mailing list