Many thanks for the replies to my call for help this morning. I didn't know about return() and that helped quite a bit.
Best, Mark On Fri, Dec 11, 2009 at 10:00 AM, Paul Hiemstra <p.hiems...@geo.uu.nl>wrote: > Hi Mark, > > This question would probably be better suited for the r-sig-geo mailing > list. In addition, please read the posting guide and provide a piece of code > that reproduces the problem. > > library(sp) > > > convert<-function(d) { > d<-data.frame(d); #convert object to dataframe > d<-subset(d,select=c(zinc,x,y)) #select some columns > d # <- add this, or alternatively 'return(d)' > } > > data(meuse) > coordinates(meuse) = ~x+y > > convert(meuse) > > But maybe better, subsetting a SPDF can be done using: > > meuse["zinc"] # Remains an SPDF > # Returns a data.frame > data.frame(coordinates(meuse), zinc = meuse$zinc) > > And some unrequested advice :). To process multiple files, take a look at > lapply, both for reading and processing. > > all_data = lapply(list_of_files, function(file) { > bla = read.table(file) > coordinates(bla) = ~coor.x1 + coor.x2 > return(bla) > } > # all data is now a list wit the SPDF's > > processed_data = lapply(all_data, function(dat) { > return(data.frame(coordinates(dat), zinc = dat$zinc)) > } > > ofcourse you can include the latter lapply stuff inside the first 'loading' > lapply. > > all_data = lapply(list_of_files, function(file) { > bla = read.table(file) > bla = subset(bla, select = select=c(time,coords.x1,coords.x2)) > coordinates(bla) = ~coor.x1 + coor.x2 > return(bla) > } > > hope this helps and good luck, > > Paul > > Mark Na wrote: > >> Hello, >> >> I am learning how to use functions, but I'm running into a roadblock. >> >> I would like my function to do two things: 1) convert an object to a >> dataframe, 2) and then subset the dataframe. Both of these commands work >> fine outside the function, but I would like to wrap them in a function so >> I >> can apply the code iteratively to many such objects. >> >> Here's what I wrote, but it doesn't work: >> >> convert<-function(d) { >> d<-data.frame(d); #convert object to dataframe >> d<-subset(d,select=c(time,coords.x1,coords.x2)) #select some columns >> } >> convert(data) #the problem is that "data" is the same as it was before >> running the function >> >> The objects being processed through my function are >> SpatialPointsDataFrames >> but I'm quite sure that's not my problem, as I can process these outside >> of >> the function (using the above code) ... it's when I try to wrap the code >> in >> a function that it doesn't work. >> >> Thanks, Mark >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> 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. >> >> > > > -- > Drs. Paul Hiemstra > Department of Physical Geography > Faculty of Geosciences > University of Utrecht > Heidelberglaan 2 > P.O. Box 80.115 > 3508 TC Utrecht > Phone: +3130 274 3113 Mon-Tue > Phone: +3130 253 5773 Wed-Fri > http://intamap.geo.uu.nl/~paul <http://intamap.geo.uu.nl/%7Epaul> > > -- Mark Na University of Saskatchewan Saskatoon, Canada [[alternative HTML version deleted]] ______________________________________________ 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.