Dear R-users I wrote a small program for assigning a membership Here is my script sample.size <- 60 x <- rnorm(sample.size, 0, 1) y <- rnorm(sample.size, 0, 1) x.mean <- mean(x) y.mean <- mean(y) membership <- numeric(sample.size) for (i in 1:sample.size) { if ((x[i] < x.mean) && (y[i] < y.mean)) { membership[i] <<- 1 } else { if ((x[i] > x.mean) && (y[i] < y.mean)) { membership[i] <<- 2 } else { if ((x[i] > x.mean) && (y[i] > y.mean)) { membership[i] <<- 3 } else { membership[i] <<- 4 } } } } cbind(x,y,membership) There is an error message "Error: object "membership" not found" I can''t figure it out. Any help or advice on improvement for this code will be appreciated. I konw this code is not well written at all. Thank you Taka

Hi, How about replacing all "<<-" with "<-"? That error occured in> membership[i] <<- 1this line and this code stopped before> cbind(x,y,membership)this line. Hope this may help you. --- W. Yamamoto> Dear R-users > > I wrote a small program for assigning a membership > > Here is my script > > sample.size <- 60 > > x <- rnorm(sample.size, 0, 1) > y <- rnorm(sample.size, 0, 1) > > x.mean <- mean(x) > y.mean <- mean(y) > membership <- numeric(sample.size) > > for (i in 1:sample.size) > { > if ((x[i] < x.mean) && (y[i] < y.mean)) > { > membership[i] <<- 1 > } else { > if ((x[i] > x.mean) && (y[i] < y.mean)) > { > membership[i] <<- 2 > } else { > if ((x[i] > x.mean) && (y[i] > > y.mean)) > { > membership[i] <<- 3 > } else > { > membership[i] <<- 4 > } > } > > } > > } > cbind(x,y,membership) > > There is an error message > "Error: object "membership" not found" > I can''t figure it out. > > Any help or advice on improvement for this code will be appreciated. > I konw this code is not well written at all. > > Thank you > > Taka > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >

The problem can be reduced to this: x <- 1 x[1] <<- 2 # error The following are ok: x <- 1 x[1] <- 3 x <- 1 x <- 4 x <- 1 x <<- 5 Does anyone know why? Is this a bug in <<- ? On 7/9/06, Taka Matzmoto <sell_mirage_ne at hotmail.com> wrote:> Dear R-users > > I wrote a small program for assigning a membership > > Here is my script > > sample.size <- 60 > > x <- rnorm(sample.size, 0, 1) > y <- rnorm(sample.size, 0, 1) > > x.mean <- mean(x) > y.mean <- mean(y) > membership <- numeric(sample.size) > > for (i in 1:sample.size) > { > if ((x[i] < x.mean) && (y[i] < y.mean)) > { > membership[i] <<- 1 > } else { > if ((x[i] > x.mean) && (y[i] < y.mean)) > { > membership[i] <<- 2 > } else { > if ((x[i] > x.mean) && (y[i] > > y.mean)) > { > membership[i] <<- 3 > } else > { > membership[i] <<- 4 > } > } > > } > > } > cbind(x,y,membership) > > There is an error message > "Error: object "membership" not found" > I can''t figure it out. > > Any help or advice on improvement for this code will be appreciated. > I konw this code is not well written at all. > > Thank you > > Taka > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >

On Mon, 10 Jul 2006, Gabor Grothendieck wrote:> The problem can be reduced to this: > > x <- 1 > x[1] <<- 2 # error > > The following are ok: > > x <- 1 > x[1] <- 3 > > x <- 1 > x <- 4 > > x <- 1 > x <<- 5 > > Does anyone know why? Is this a bug in <<- ?No, it''s a feature. The fact that x<<-5 works is arguably a bug (though probably not worth fixing). x[1] <<- 2 is equivalent (per section 3.4.4 of the language guide) to `*tmp*` <- get("x", envir=parent.env(), inherits=TRUE) `*tmp*`[1] <- 2 x <<- `*tmp*` and the get() fails when you try to do this from the command line. Since the point of superassignment is to assign in a lexical parent environment it makes no sense to do it directly at the command line. -thomas Thomas Lumley Assoc. Professor, Biostatistics tlumley at u.washington.edu University of Washington, Seattle