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) > rpointsError: object "rpoints" not found # test spatial data library(splancs) data(cardiff) attach(cardiff) str(cardiff) events <- as.points(x,y) ### non-working function #### 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)) } }
On 10/11/2007 2:17 PM, 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 > > # test spatial data > library(splancs) > data(cardiff) > attach(cardiff) > str(cardiff) > events <- as.points(x,y) > > ### non-working function #### > > 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)) > } > }You generate rpoints within the scope of the function, but you don't return it. Change your function to 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)) } rpoints } and call it as rpoints <- gen.rpoints(...) (There are ways to create variables outside of the scope of the function, but you shouldn't use them.) Duncan Murdoch
You need to return the value from the function. "rpoints" is local to the function and not visible. 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)) } rpoints } rpoints <- gen.rpoints(events, poly, 99) On 10/11/07, Dale Steele <dale.w.steele at gmail.com> 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 > > # test spatial data > library(splancs) > data(cardiff) > attach(cardiff) > str(cardiff) > events <- as.points(x,y) > > ### non-working function #### > > 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)) > } > } > > ______________________________________________ > 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 Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve?
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 foundLooks 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 this: rppoints <- gen.rpoints(events, poly, 99) cu Philipp -- 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 and Institut f?r Bioinformatik / MIPS GSF - Forschungszentrum f?r Umwelt und Gesundheit Ingolst?dter Landstrasse 1 85764 Neuherberg, Germany http://mips.gsf.de/staff/pagel