[R] pass by reference

rest rest at digipsy.nl
Tue Aug 14 10:00:03 CEST 2012


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.



More information about the R-help mailing list