Hi all, I want to do the following: data<-data.frame(col1=c(1,2,3,4,5)) getcol2<-function(data){ data$col2[data$col1<=2]="L" } getcol2(data) Unfortunately in the above col2 does not appear in the final data. So how would you pass this by reference such that you would get it back? Thanks, Sachin [[alternative HTML version deleted]]
You have to return the value of 'data' from the function. Functions do not have "side effects".> data<-data.frame(col1=c(1,2,3,4,5)) > > getcol2<-function(data){+ data$col2[data$col1<=2]="L" + data # return value + }> > getcol2(data)col1 col2 1 1 L 2 2 L 3 3 <NA> 4 4 <NA> 5 5 <NA>>On Mon, Aug 13, 2012 at 9:08 PM, Sachinthaka Abeywardana <sachin.abeywardana at gmail.com> wrote:> Hi all, > > I want to do the following: > > data<-data.frame(col1=c(1,2,3,4,5)) > > getcol2<-function(data){ > data$col2[data$col1<=2]="L" > } > > getcol2(data) > > Unfortunately in the above col2 does not appear in the final data. So how > would you pass this by reference such that you would get it back? > > Thanks, > Sachin > > [[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.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
HI, Try this: getcol2<-function(data){ ??? data$col2[data$col1<=2]="L" ?data ?} data<-getcol2(data) ?data #? col1 col2 #1??? 1??? L #2??? 2??? L #3??? 3 <NA> #4??? 4 <NA> #5??? 5 <NA> A.K. ----- Original Message ----- From: Sachinthaka Abeywardana <sachin.abeywardana at gmail.com> To: r-help at r-project.org Cc: Sent: Monday, August 13, 2012 9:08 PM Subject: [R] pass by reference Hi all, I want to do the following: data<-data.frame(col1=c(1,2,3,4,5)) getcol2<-function(data){ ? ? data$col2[data$col1<=2]="L" } getcol2(data) Unfortunately in the above col2 does not appear in the final data. So how would you pass this by reference such that you would get it back? Thanks, Sachin ??? [[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.
Hi Sachinthaka, You can do it in the following way: getcol2<-function(data){ data$col2[data$col1<=2]="L" data<<-data } <<- writes the result to the underlying environment. This is however generally seen as very bad programming (side effects). Greet' Frans-----Oorspronkelijk bericht----- Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Sachinthaka Abeywardana Verzonden: dinsdag 14 augustus 2012 3:08 Aan: r-help at r-project.org Onderwerp: [R] pass by reference Hi all, I want to do the following: data<-data.frame(col1=c(1,2,3,4,5)) getcol2<-function(data){ data$col2[data$col1<=2]="L" } getcol2(data) Unfortunately in the above col2 does not appear in the final data. So how would you pass this by reference such that you would get it back? Thanks, Sachin [[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.
You can use macros for this effect. Or environments: daf <- data.frame(a=1:10, b=rnorm(10)) env <- as.environment(daf) fun <- function(x) x$c <- x$a+x$b fun(daf) fun(env) daf$c env$c You can see that the same function (fun) changes one object but leaves another one unchanged. But before using it for something important, think of the side effects and efficiency. Passing by value does not create as many unnecessary copies as one might think. On Tue, Aug 14, 2012 at 4:08 AM, Sachinthaka Abeywardana <sachin.abeywardana at gmail.com> wrote:> Hi all, > > I want to do the following: > > data<-data.frame(col1=c(1,2,3,4,5)) > > getcol2<-function(data){ > data$col2[data$col1<=2]="L" > } > > getcol2(data) > > Unfortunately in the above col2 does not appear in the final data. So how > would you pass this by reference such that you would get it back? > > Thanks, > Sachin > > [[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.