Dear R Helpers, I have about 20 data frames that I need to do a series of data scrubbing steps to. I have the list of data frames in a list so that I can use lapply. I am trying to build a function that will do the data scrubbing that I need. However, I am new to functions and there is something fundamental that I am not understanding. I use the return function at the end of the function and this completes the data processing specified in the function, but leaves the data frame that I want changed unaffected. How do I get my function to apply its results to the data frame in question instead of simply displaying the results to the screen? Any helpful guidance would be most appreciated. --John Sparks x=as.data.frame(matrix(c(1,2,3, 1,2,3, 1,2,2, 1,2,2, 1,1,1),ncol=3,byrow=T)) myfunc<-function(DF){ DF<-subset(DF,select=-c(V1)) return(DF) } myfunc(x) #How to get this change to data frame x? #And preferrably not send the results to the screen? x
Hi, If I understand it correctly, x<-myfunc(x) x #? V2 V3 #1? 2? 3 #2? 2? 3 #3? 2? 2 #4? 2? 2 #5? 1? 1 A.K. ----- Original Message ----- From: "Sparks, John James" <jspark4 at uic.edu> To: r-help at r-project.org Cc: Sent: Monday, April 29, 2013 10:23 AM Subject: [R] Function for Data Frame Dear R Helpers, I have about 20 data frames that I need to do a series of data scrubbing steps to.? I have the list of data frames in a list so that I can use lapply.? I am trying to build a function that will do the data scrubbing that I need.? However, I am new to functions and there is something fundamental that I am not understanding.? I use the return function at the end of the function and this completes the data processing specified in the function, but leaves the data frame that I want changed unaffected. How do I get my function to apply its results to the data frame in question instead of simply displaying the results to the screen? Any helpful guidance would be most appreciated. --John Sparks x=as.data.frame(matrix(c(1,2,3, ? ? ? ? 1,2,3, ? ? ? ? 1,2,2, ? ? ? ? 1,2,2, ? ? ? 1,1,1),ncol=3,byrow=T)) myfunc<-function(DF){ DF<-subset(DF,select=-c(V1)) return(DF) } myfunc(x) #How to get this change to data frame x? #And preferrably not send the results to the screen? x ______________________________________________ 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.
Hi, On Apr 29, 2013, at 10:23 AM, Sparks, John James wrote:> Dear R Helpers, > > I have about 20 data frames that I need to do a series of data scrubbing > steps to. I have the list of data frames in a list so that I can use > lapply. I am trying to build a function that will do the data scrubbing > that I need. However, I am new to functions and there is something > fundamental that I am not understanding. I use the return function at the > end of the function and this completes the data processing specified in > the function, but leaves the data frame that I want changed unaffected. > How do I get my function to apply its results to the data frame in > question instead of simply displaying the results to the screen? > > Any helpful guidance would be most appreciated. > > --John Sparks > > > x=as.data.frame(matrix(c(1,2,3, > 1,2,3, > 1,2,2, > 1,2,2, > 1,1,1),ncol=3,byrow=T)) > > > myfunc<-function(DF){ > DF<-subset(DF,select=-c(V1)) > return(DF) > } > > myfunc(x) > > #How to get this change to data frame x? > #And preferrably not send the results to the screen? > x >Good question! In your example, x is passed into myfunc by value (a copy of the value of x) rather than by reference (like passing in the social security number of x). So your scrubbing within the function is done on a copy of x, which you call DF. To update the value of x outside of your function, you have to assign the returned value of myfunc to x x <- myfunc(x) See more at ... http://cran.r-project.org/doc/manuals/r-release/R-intro.html#Writing-your-own-functions Cheers, Ben> ______________________________________________ > 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.Ben Tupper Bigelow Laboratory for Ocean Sciences 60 Bigelow Drive, P.O. Box 380 East Boothbay, Maine 04544 http://www.bigelow.org
Just to add a little, don't get distracted by the return() function. Functions return the value of their final expression, provided it isn't an assignment. For your example, this will do the job: myfunc <- function(DF) subset(DF, select=-V1) If you want to modify the data frames in place, one way is to use a loop instead of lapply. mydfs <- list(DF1, DF2, DF3) for (il in 1:3) mydfs[[il]] <- myfunc(mydfs[[il]]) But so should mydfs <- lapply(mydfs,myfunc) I doubt very much you'll see any performance difference between using lapply() and using an explicit loop. -Don -- Don MacQueen Lawrence Livermore National Laboratory 7000 East Ave., L-627 Livermore, CA 94550 925-423-1062 On 4/29/13 7:23 AM, "Sparks, John James" <jspark4 at uic.edu> wrote:>Dear R Helpers, > >I have about 20 data frames that I need to do a series of data scrubbing >steps to. I have the list of data frames in a list so that I can use >lapply. I am trying to build a function that will do the data scrubbing >that I need. However, I am new to functions and there is something >fundamental that I am not understanding. I use the return function at the >end of the function and this completes the data processing specified in >the function, but leaves the data frame that I want changed unaffected. >How do I get my function to apply its results to the data frame in >question instead of simply displaying the results to the screen? > >Any helpful guidance would be most appreciated. > >--John Sparks > > >x=as.data.frame(matrix(c(1,2,3, > 1,2,3, > 1,2,2, > 1,2,2, > 1,1,1),ncol=3,byrow=T)) > > >myfunc<-function(DF){ > DF<-subset(DF,select=-c(V1)) > return(DF) >} > >myfunc(x) > >#How to get this change to data frame x? >#And preferrably not send the results to the screen? >x > >______________________________________________ >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.