[R] Please help with a basic function

Ista Zahn istazahn at gmail.com
Fri Dec 11 16:20:27 CET 2009


Hi Mark,
You're on the right track. You just need your function to return dataframe. Try

convert<-function(d) {
 d<-data.frame(d); #convert object to dataframe
 d<-subset(d,select=c(time,coords.x1,coords.x2)) #select some columns
return(d)
}

-Ista

On Fri, Dec 11, 2009 at 9:19 AM, Mark Na <mtb954 at gmail.com> 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 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.
>



-- 
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org




More information about the R-help mailing list