[R] dataframes from a function

Phil Spector spector at stat.berkeley.edu
Fri Feb 4 19:29:43 CET 2011


Chris -
    You can solve your problem by removing the print 
statements and replacing them with

answer = list(org.plot[org.plot$sub %in% ex.plot,],
               new.pl[new.pl$yar %in% "1991",],
               new.pl[new.pl$yar %in% "1993",])
prefix = sub('-','.',ex.plot)
names(answer) = paste(prefix,c('.new','new1991','new1993'),sep='')
return(answer)

That will return a list with three dataframes having the names that
you specified.

If you actually want to create separate objects in your workspace, you
could look at the help page for the assign function.

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu




On Fri, 4 Feb 2011, chris20 wrote:

>
> Hi,
> I'm trying to create a function to return three dataframes for later use in
> a graphic so I want the return from the function to give me dataframes and
> with unique names relating to the variable they are based on.
> For example.....
>
> sub<-c("6-1a","6-1a","6-1a","9-2b","9-2b","9-2b","7c","7c","7c")
> yar<-rep(1991:1993,3)
> x1n<-c(4,6,3,6,4,7,9,4,2)
> y1m<-c(3,7,5,8,4,3,6,7,8)
> prac<-data.frame(sub,yar,x1n,y1m)
>
> plot.restrut<-function(org.plot,ex.plot) {
> new.pl<-org.plot[org.plot$sub %in% ex.plot,]
> new.pl.st<-new.pl[new.pl$yar %in% "1991",]
> new.pl.fin<-new.pl[new.pl$yar %in% "1993",]
> print(new.pl)
> print(new.pl.st)
> print(new.pl.fin)
> }
>
> plot.restrut(prac,"9-2b")
>
> ######
> I would like the output to be one dataframe with the name "9.2b.new" which
> is new.pl in the example
> Another data frame to be "9.2bnew1991" and another dataframe "9.2bnew1993"
>
> I want the dataframe names to relate to the variables they have come from
> because I want to use the same function to extract lots of subsets of my
> data.
>
> Thanks
> Chris
>
>
> -- 
> View this message in context: http://r.789695.n4.nabble.com/dataframes-from-a-function-tp3260581p3260581.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>



More information about the R-help mailing list