[R] variable (column) in a data frame

Sarah Goslee sarah.goslee at gmail.com
Tue Jul 17 17:04:50 CEST 2012


Hi,

On Tue, Jul 17, 2012 at 10:40 AM, Paulo Barata
<paulo.barata at ensp.fiocruz.br> wrote:
>
> Dear Frans and Peter,
>
> Yes, the notation df[,'var'] is able to catch a non-existent
> variable var inside a data frame df. But the notation df$var
> isn't.
>
> So we have this situation, where two different notations, which
> (as far as I understand) perform the same action, have different
> kinds of response.

But they don't perform the same action: the defaults are different.
This is documented, although verbosely and somewhat confusingly, see
for instance ?"$" and pay particular attention to the sections on
partial matching.

> Couldn't this situation be fixed? Isn't it possible to make the
> df$var notation to issue an error when referring to a non-existent
> variable inside the data frame?

Not without completely changing the way partial matching is handled.
The answer has already been offered: don't use $ - it's really just
there as a shortcut, and like all shortcuts has attendant risks not
found on the longer, smoother main road.

Sarah

> Thank you very much.
>
> Paulo Barata
>

-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list