[R] What is wrong with m?

Douglas Bates bates at stat.wisc.edu
Fri Sep 19 20:28:08 CEST 2003


Spencer Graves <spencer.graves at pdf.com> writes:

> For "m[1] <- ..." to work, "m" must already be defined.  To fix this,
> execute "m <- rep(NA, index)" before your second "for" loop, and it
> should work. hope this helps. spencer graves

If saveList is actually a list, it would be more effective to use

 m = sapply(saveList, median)

That way there is no need to generate m and then change each element
of m in a loop.

One neglected aspect of the S language is the ability to apply
functions to structures instead of iterating over the components of a
structure.  Phil Spector's book "An Introduction to S and S-PLUS"
emphasizes this.

> Anna Pryor wrote:
> 
> > I've been programming in one directory and recently switched to
> > another directory.  It appears that in doing so I've uncovered a
> > problem.  My environment was saving something so that my code would
> > work and now I don't know how to fix it.  I have the following bit
> > of code:
> 
> >
> >  for(i in 1:index){
> >    indexList = lst[i]
> >	for(j in 2:jobs-1){
> >           indexList = c(indexList,lst[i+index*j])
> >        }
> >   saveList[i,] = indexList
> >
> >   }
> >
> >
> >   for (i in 1:index){
> >	m[i] = median(saveList[i,])
> >
> >}
> >m
> >
> > In my new directory, I am getting the complaint that "object m is
> > not found."  Does anyone know what that means?
> 
> >
> >Anna
> >
> >______________________________________________
> >R-help at stat.math.ethz.ch mailing list
> >https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> >
> 
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help

-- 
Douglas Bates                            bates at stat.wisc.edu
Statistics Department                    608/262-2598
University of Wisconsin - Madison        http://www.stat.wisc.edu/~bates/




More information about the R-help mailing list