Skip to content
Prev 303286 / 398506 Next

pass by reference

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.abeywardana at gmail.com>
To: jim holtman <jholtman at gmail.com>
Cc: r-help at 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 <jholtman at gmail.com> wrote:

            
??? [[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.