Matt Strauser
2013-Sep-03 14:19 UTC
[R] How to assign names to global data frames created in a function
I have several data frames containing similar data. I'd like to pass these data frames to a function for processing. The function would create newly named "global" data frames containing the processed data. I cannot figure out how to assign names to the data frames in Step 1 or Step 2 in the following example: # sample function in pseudo code processdf <- function(df, prefix) { # df - data frame containing data for processing # prefix - string to become the first part of the names of the resulting data frames # Step 1 - processs df into several subsets df1 <<- subset(df, df$cond1 & df$cond2 & ...) df2 <<- subset(df, df$cond3 & df$cond4 & ...) df3 <<- subset(df, df$cond5 & df$cond6 & ...) # and so on....for many more steps with resulting data frames # Step 2 - rename the resulting global data frames rename "df1" to prefix + "cond1cond2" rename "df2" to prefix + "cond3cond4" rename "df3" to prefix + "cond5cond6" # and so on for the remaining data frames } Example using data frames: frame1 and frame2: processdf(frame1, "frame1") # produces these data frames: frame1cond1cond2 frame1cond3cond4 frame1cond5cond6 processdf(frame2, "frame2") # produces these data frames: frame2cond1cond2 frame2cond3cond4 frame2cond5cond6 Thank you for your thoughts, Matt [[alternative HTML version deleted]]
David Winsemius
2013-Sep-03 20:09 UTC
[R] How to assign names to global data frames created in a function
On Sep 3, 2013, at 7:19 AM, Matt Strauser wrote:> I have several data frames containing similar data. I'd like to pass these > data frames to a function for processing. The function would create newly > named "global" data frames containing the processed data. I cannot figure > out how to assign names to the data frames in Step 1 or Step 2 in the > following example: > > # sample function in pseudo code > processdf <- function(df, prefix) { > # df - data frame containing data for processing > # prefix - string to become the first part of the names of the resulting > data frames > # Step 1 - processs df into several subsets > df1 <<- subset(df, df$cond1 & df$cond2 & ...) > df2 <<- subset(df, df$cond3 & df$cond4 & ...) > df3 <<- subset(df, df$cond5 & df$cond6 & ...) > # and so on....for many more steps with resulting data framesGenerally subset does not need to have 'df$cond1' used. It uses nonstandard evaluation and will assume that unquoted tokens are column names, so this can be wrtten:> df1 <- subset(df, cond1 & cond2 ) > df2 <- subset(df, cond3 & cond4) > df3 <- subset(df, cond5 & cond6 )There is, however, this warning in the help file: "For programming it is better to use the standard subsetting functions like `[`, and in particular the non-standard evaluation of argument subset can have unanticipated consequences." A similar warning is seen on the help page for 'with'. So this would be advised df1 <- df[ df[, 'cond1'] & df[,'cond2'], ]> > # Step 2 - rename the resulting global data frames > rename "df1" to prefix + "cond1cond2"?assign # e.g.: # untested in absence of reproducible example # You should learn to use dput() assign(paste0(prefix, "cond1", "cond2"), df1, envir=.GlobalEnv) And please read the manual of your chosen mail-client and learn to post to Rhelp in plain text. -- David> rename "df2" to prefix + "cond3cond4" > rename "df3" to prefix + "cond5cond6" > # and so on for the remaining data frames > } > > Example using data frames: frame1 and frame2: > > processdf(frame1, "frame1") > # produces these data frames: > frame1cond1cond2 > frame1cond3cond4 > frame1cond5cond6 > > processdf(frame2, "frame2") > # produces these data frames: > frame2cond1cond2 > frame2cond3cond4 > frame2cond5cond6 > > Thank you for your thoughts, > Matt > > [[alternative HTML version deleted]]David Winsemius Alameda, CA, USA
Greg Snow
2013-Sep-03 21:57 UTC
[R] How to assign names to global data frames created in a function
This is FAQ 7.21. The most important part of that FAQ is at the end where it says it is best not to do this and better to use lists. Having a function create (overwrite) objects in the global environment is dangerous and can lead to hard to find bugs. It would be better to have your function return a list containing the data frames, is there any reason that that would not work for your case? On Tue, Sep 3, 2013 at 8:19 AM, Matt Strauser <syntereo@gmail.com> wrote:> I have several data frames containing similar data. I'd like to pass these > data frames to a function for processing. The function would create newly > named "global" data frames containing the processed data. I cannot figure > out how to assign names to the data frames in Step 1 or Step 2 in the > following example: > > # sample function in pseudo code > processdf <- function(df, prefix) { > # df - data frame containing data for processing > # prefix - string to become the first part of the names of the resulting > data frames > # Step 1 - processs df into several subsets > df1 <<- subset(df, df$cond1 & df$cond2 & ...) > df2 <<- subset(df, df$cond3 & df$cond4 & ...) > df3 <<- subset(df, df$cond5 & df$cond6 & ...) > # and so on....for many more steps with resulting data frames > > # Step 2 - rename the resulting global data frames > rename "df1" to prefix + "cond1cond2" > rename "df2" to prefix + "cond3cond4" > rename "df3" to prefix + "cond5cond6" > # and so on for the remaining data frames > } > > Example using data frames: frame1 and frame2: > > processdf(frame1, "frame1") > # produces these data frames: > frame1cond1cond2 > frame1cond3cond4 > frame1cond5cond6 > > processdf(frame2, "frame2") > # produces these data frames: > frame2cond1cond2 > frame2cond3cond4 > frame2cond5cond6 > > Thank you for your thoughts, > Matt > > [[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. >-- Gregory (Greg) L. Snow Ph.D. 538280@gmail.com [[alternative HTML version deleted]]
arun
2013-Sep-03 22:12 UTC
[R] How to assign names to global data frames created in a function
Hi, May be this helps you in getting started. set.seed(29) ?df1<- as.data.frame(matrix(sample(1:20,5*10,replace=TRUE),5,10)) ?cond<- c("V1eq2","V8eq2","V6eq4orV8eq7") fun1<- function(df,prefix,cond){ lst1<- list(df[df$V1==2,],df[df$V8==2,],df[df$V6==4|df$V8==7,]) for(i in seq_along(cond)){ ?assign(paste(prefix,cond[i],sep="_"),lst1[[i]],envir=.GlobalEnv) ?print(paste(prefix,cond[i],sep="_"))} ?} fun1(df1,"frame1",cond) #[1] "frame1_V1eq2" #[1] "frame1_V8eq2" #[1] "frame1_V6eq4orV8eq7" ?frame1_V1eq2 #? V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 #1? 2? 2 20 17 14? 8? 8? 2 13? 18 ?frame1_V8eq2 #? V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 #1? 2? 2 20 17 14? 8? 8? 2 13? 18 #5 12? 5? 4? 8 13? 4 10? 2? 2? 13 ?frame1_V6eq4orV8eq7 #? V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 #3? 3 18? 7? 8 20 18? 2? 7 20? 15 #4? 7? 3 13 18 20 14 15? 7 14? 12 #5 12? 5? 4? 8 13? 4 10? 2? 2? 13 A.K. ----- Original Message ----- From: Matt Strauser <syntereo at gmail.com> To: r-help at r-project.org Cc: Sent: Tuesday, September 3, 2013 10:19 AM Subject: [R] How to assign names to global data frames created in a function I have several data frames containing similar data. I'd like to pass these data frames to a function for processing. The function would create newly named "global" data frames containing the processed data. I cannot figure out how to assign names to the data frames in Step 1 or Step 2 in the following example: # sample function in pseudo code processdf <- function(df, prefix) { # df - data frame containing data for processing # prefix - string to become the first part of the names of the resulting data frames # Step 1 - processs df into several subsets ? df1 <<- subset(df, df$cond1 & df$cond2 & ...) ? df2 <<- subset(df, df$cond3 & df$cond4 & ...) ? df3 <<- subset(df, df$cond5 & df$cond6 & ...) # and so on....for many more steps with resulting data frames # Step 2 - rename the resulting global data frames ? rename "df1" to prefix + "cond1cond2" ? rename "df2" to prefix + "cond3cond4" ? rename "df3" to prefix + "cond5cond6" # and so on for the remaining data frames } Example using data frames: frame1 and frame2: processdf(frame1, "frame1") # produces these data frames: frame1cond1cond2 frame1cond3cond4 frame1cond5cond6 processdf(frame2, "frame2") # produces these data frames: frame2cond1cond2 frame2cond3cond4 frame2cond5cond6 Thank you for your thoughts, Matt ??? [[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.