HI, CPU time used: n<-1e6 system.time({ set.seed(1) dat1<-data.frame(col1=sample(1:5,n,replace=TRUE)) getcol2<-function(dat1){ dat1$col2[dat1$col1<=2]="L" dat1 } dat1<-getcol2(dat1) })
#user system elapsed # 0.096 0.000 0.095 system.time({ set.seed(1) dat1<-data.frame(col1=sample(1:5,n,replace=TRUE)) dat2<-within(dat1,{col2<-ifelse(col1<=2,"L",NA)}) }) # user system elapsed # 0.580 0.012 0.593 library(car) system.time({ set.seed(1) dat1<-data.frame(col1=sample(1:5,n,replace=TRUE)) x<-dat1$col1 dat1$col2<-recode(x,'0="L";1="L";2="L"') }) #user system elapsed # 0.548 0.004 0.553 A.K. ----- Original Message ----- From: Sachinthaka Abeywardana <sachin.abeyward...@gmail.com> To: jim holtman <jholt...@gmail.com> Cc: r-help@r-project.org Sent: Monday, August 13, 2012 9:30 PM Subject: Re: [R] pass by reference Think you are missing the point, assigning the value back is the same as passing by value. This is rather inefficient if you ever have to deal with large datasets. You dont want to keep having a local copy within the scope of the function and then copying over the original. On Tue, Aug 14, 2012 at 11:25 AM, jim holtman <jholt...@gmail.com> wrote: > The assign the value back to the object: > > > data<-data.frame(col1=c(1,2,3,4,5)) > > > > getcol2<-function(data){ > + data$col2[data$col1<=2]="L" > + data # return value > + } > > > > data <- getcol2(data) # save the return value > > 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:23 PM, Sachinthaka Abeywardana > <sachin.abeyward...@gmail.com> wrote: > > Hi Jim, R, > > > > What you just showed me simply prints out the 2nd column. If you inspect > > your original data, it still just has 1 column. So its still passing by > > value. > > > > Thanks, > > Sachin > > > > On Tue, Aug 14, 2012 at 11:19 AM, jim holtman <jholt...@gmail.com> > wrote: > >> > >> 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.abeyward...@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@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. > > > > > > > > -- > 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. > [[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. ______________________________________________ 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.