[R] subset function unexpected behavior

Thomas Lumley tlumley at u.washington.edu
Tue Feb 2 18:50:29 CET 2010


On Tue, 2 Feb 2010, David Katz wrote:

>
> Thanks, that helps! Subset creates a new context where a name clash can
> occur. So if I don't want to check for that possibility, I should use a
> special kind of index like .sch, or avoid subset:
>
> for(sch in school.list){
>  print(sch)
>  print(input.data[input.data[,school.var] == sch,])}
>
> which works no matter what variable names I use. That seems like a
> reasonable requirement for good code.
>
> (Checking for a name clash would be at least theoretically needed since
> school.var is a parameter that can be any character name.)
>
> Although subset conveniently avoids extra typing in many cases (not here),
> this suggests to me that it's not ideal for code that can be used in a
> variety of contexts. Note that unlike "attach", subset does not issue a
> warning!

attach() doesn't issue a warning for this situation,  it warns when a variable in the data set *fails to* mask one in the global environment.  subset() can't fail to mask variables further up the search path, so it doesn't ever warn.

      -thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle



More information about the R-help mailing list