[R] reason for error in small function?

Philipp Pagel p.pagel at gsf.de
Thu Oct 11 21:04:40 CEST 2007

On Thu, Oct 11, 2007 at 02:17:14PM -0400, Dale Steele wrote:
> Running the function below, tested using the cardiff dataset from
> splancs generates the following error.   What changes do I need to
> make to get the function to work?  Thanks.  --Dale
> > gen.rpoints(events, poly, 99)
> > rpoints
> Error: object "rpoints" not found

Looks like the variable rpoints does not exist.

> gen.rpoints <- function(events, poly, nsim){
> rpoints <- array(0, dim=c(nrow(events),2,nsim))
>   for (i in 1:nsim) {
>     rpoints[, ,i] <- csr(poly, nrow(events))
>   }
> }

OK – so I guess this function is intended to create the variable
rpoints. And it does – but the variable exists only inside the function.
You could fix this by using the <<- operator but it is much cleaner to add
return(rppoints) as the last line of your function and then call it like

rppoints <- gen.rpoints(events, poly, 99)


Dr. Philipp Pagel                              Tel.  +49-8161-71 2131
Lehrstuhl für Genomorientierte Bioinformatik   Fax.  +49-8161-71 2186
Technische Universität München
Wissenschaftszentrum Weihenstephan
85350 Freising, Germany


Institut für Bioinformatik / MIPS
GSF - Forschungszentrum für Umwelt und Gesundheit
Ingolstädter Landstrasse 1
85764 Neuherberg, Germany

More information about the R-help mailing list