Go back and reread the section about the scoping of variables and that
functions do not have side effects; they only return values. You are
changinga "local" copy of df1 within the function which is returning
the changed values to df3.
On Wed, Jul 11, 2012 at 7:36 PM, Charles Stangor
<cstangor at charlesstangor.com> wrote:> Why does this sapply code change df3 but not df1?
>
> Thanks
>
> df1 <- read.table(text="
> cola colb colc cold cole
> 1 NA 5 9 NA 17
> 2 NA 6 NA 14 NA
> 3 3 NA 11 15 19
> 4 4 8 12 NA 20
> ", header=TRUE)
>
> df2 <-df1*2
> df1
> df2
>
> df3 <-sapply(names(df1),function(x) {df1[[x]]<- df2[[x]]})
> df1
> df3
>
> [[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.