[R] dropping variables from a data frame inside a function

Duncan Murdoch murdoch.duncan at gmail.com
Sat Jun 23 00:58:09 CEST 2012


On 12-06-22 5:09 PM, Joseph Boyer wrote:
> Why doesn't this work?

David gave you a solution that works.  The answer to this question is 
that unlike SAS, R is a computing language that has an idea of variable 
scoping:  when you modify the argument df in DropLikeSAS, you are making 
local changes, not global ones.

Duncan Murdoch

>
>                  #Drop a variable name from a data frame
>
> DropLikeSAS<- function(x,df) {
>
>                                  df[[x]]<- NULL
>
>                                  0
>
>                                  }
>
> DropLikeSAS("VarName", DataFrameName)
>
>
> Try it. The column VarName will not be deleted from the data frame DataFrameName.
>
>
> But
>
> x<- "VarName"
> DataFrameName[[x]]<- NULL
>
> Works.
>
>
>
> 	[[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