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

R. Michael Weylandt michael.weylandt at gmail.com
Sat Jun 23 00:55:08 CEST 2012


I think the OP might also be tripped up on the fact that R is
pass-by-value so effects on df inside DropLikeSAS won't have impact
outside the function's scope. The df inside of DropLikeSAS() is
changed as expected, but that has no effect on the df outside that
function.

To the OP: There are ways to get pass-by-reference behavior (which I
think is what you are expecting), but the default is pass-by-value for
good reason. Give it a shot for a while and see if you come to like it
-- I think you will.

Best,
Michael

On Fri, Jun 22, 2012 at 4:22 PM, David Winsemius <dwinsemius at comcast.net> wrote:
>
> On Jun 22, 2012, at 5:09 PM, Joseph Boyer wrote:
>
>> DropLikeSAS <- function(x,df) {
>>
>>                               df[[x]] <- NULL
>>
>>                               0
>>
>>                               }
>>
>> DropLikeSAS("VarName", DataFrameName)
>
>
> Sorry for the blank message>
>
>> DropLikeSAS <- function(x,df) {
> +                                df[[x]] <- NULL
> +                               return(df) }
>>
>> DropLikeSAS("b", df)
>    a
> 1   1
> 2   2
> 3   3
> 4   4
> 5   5
> 6   6
> 7   7
> 8   8
> 9   9
> 10 10
>
>>
>
>
> David Winsemius, MD
> West Hartford, CT
>
> ______________________________________________
> 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