[R] modify a data.frame within a function

Mark Wardle mark at wardle.org
Sat Dec 15 16:13:25 CET 2007


You need to read about scope.

Try using return() from within your function to return the modified
data frame to the caller.

Best wishes,

Mark

On 15/12/2007, Yuval Sapir <ysapir at bgu.ac.il> wrote:
>  Hello all,
> I'm trying to modify a single column of a data frame to remove randomly half of the values. I want to do it within a function, but can not assign the modified column back into the data frame. It was easy and successful without a function, so I suspect the problem is the call of the single column within the function.
>
> removedata<-function(datafull,var.removed)
> {
> attach(datafull)
> NArandom<-rnorm(n=length(var.removed), mean=0, sd=1)
> for (i in 1:length(var.removed))
>   if(NArandom[i]>0)
>     var.removed[i]<-"NA"
> datamiss<<-datafull
> detach(datafull)
> }
>
> -- Yuval Sapir, PhDResearcherInstitute of Evolution, Haifa UniversityHaifa, Israel 3190‎
>
>         [[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.
>
>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email
> ______________________________________________________________________
>


-- 
Dr. Mark Wardle
Specialist registrar, Neurology
Cardiff, UK


More information about the R-help mailing list