Marcio
Looking at the script (not much explanation re your intention), I think
there is a couple of problems:
1. Not sure if the attached was supposed to be working code, but the
assignment operator is <- not <=
2. The functions should be defined OUTSIDE the loop, otherwise you are
redefining the function with every iteration
3. I think the reason you wanted to put it inside the loop is that you need
access to the local variables of the loop, i.e. i and j
    The way to do that would be to pass i and j as parameters to your
function, for example

g2 <- function(gen, j, i) sample (b1, 1, re = T)

4. Your function g1 returns a matrix and function g2 returns a number, these
should ideally both return objects of the same dimension and class.  I think
this is what you wanted

g1 <- function(gen, j, i) {
                if (gen[j, i] == 0) al1 [i, 1] + al1 [i, 1]
                else if (gen [j, i] == 1) al1 [i, 1] +  al2 [i, 1]
                else if (gen [j, i] == 2) al2 [i, 1] +  al2 [i, 1]
                else 999
                }

5. To make people that respond's life easier, please also provide some code
to initialise variables

Here is working code, which I think does what you wanted:
#######################################################
# initialising some variables
loc=200
ind=1000
gen <- matrix(rbinom(1000*200,1,0.3),nrow = ind,ncol = loc)
al1 <- matrix(rbinom(1000*200,1,0.3),nrow = ind,ncol = loc)
al2 <- matrix(rbinom(1000*200,1,0.3),nrow = ind,ncol = loc)
b1 <- c(1,2,3)
r2 = 0.7
g <- matrix(nrow = ind,ncol = loc)

# defining the functions
g1 <- function(gen, j, i) {
                if (gen[j, i] == 0) al1 [i, 1] + al1 [i, 1]
                else if (gen [j, i] == 1) al1 [i, 1] +  al2 [i, 1]
                else if (gen [j, i] == 2) al2 [i, 1] +  al2 [i, 1]
                else 999
                }
g2 <- function(gen, j, i) sample (b1, 1, re = T)

# now loop  - this can be done more efficiently, but did not want to change
your code too much :-)
for (i in 1 : loc) {
    for (j in 1 : ind) {
        xx <- if (runif(1) >= (1 - r2)) g1 else g2
        g[j, i] <- xx(gen, j, i)
    }
}

##############################################################################

HTH
Schalk Heunis


On Fri, Sep 18, 2009 at 4:50 AM, Marcio Resende <mresendeufv@yahoo.com.br>wrote:

>
> I am new in R and i am having trouble here. I´ve already searched in the
> list
> but hasn´t helped
> When i run this script above i get the message "Error in gen[j, i] :
> incorrect number of dimensions". However gen is 1000x200 (ind x loc) and so
> is g
>
> could anybody help me
>
> for (i in 1 : loc) {      #loc=200
> for (j in 1 : ind) {      #ind=1000
>
> g1 <= function ( gen ) matrix ( if (gen[j, i] == 0) al1 [i, 1] + al1 [i, 1]
> else if (gen [j, i] == 1) al1 [i, 1]       +  al2 [i, 1] else if  (gen [j,
> i] == 2) al2 [i, 1] + al2 [i, 1] else 999, ncol = loc, nrow = ind)
>
>
> g2 <= function ( gen ) sample (b1, 1, re = T)  #b1 is 1x3 and came from a
> vector 1000X1 (e.g b1 <- c(x     [1000,1]...)
>
> xx <= if (runif (1) >= (1 - r2)) g1 else g2
>
> g [j, i] <= xx (gen [j, i])    #g was already generated as an 0 matrix
> (1000x200) and i would like to replace ##by those functios
>
> }
> }
>
> Thank you very much
> --
> View this message in context:
> http://www.nabble.com/Incorrect-Dimension-tp25502336p25502336.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help@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]]


