Skip to content

accessing elements located after $ symbol

3 messages · Lorenzo Cattarino, Joshua Wiley, Ben Bolker

#
Dear Lorenzo,

This is the trade off that comes with convenience.  The `$` operator
passes its argument directly as I understand it.  This is what lets
you pass unquoted names that not variables.  The way around it is to
use the `[` extraction operator.  Look at these examples:

test[interest]
#or
test[, interest]
# but
test[first]
test[,first]

Notice that for `[`, the name of the column _must_ be quoted, or be an
object itself.  Typing:

test$interest

is trying to look up the 'interest' column, which does not exist, and
is equivalent to:  test[,"interest"] which is clearly not what you
want.

HTH,

Josh

P.S. I am sure there are others who could provide a more detailed
description.  `$` is primitive and I am only used to R code, so I have
never actually looked through its source.

On Mon, Oct 4, 2010 at 10:45 PM, Lorenzo Cattarino
<l.cattarino at uq.edu.au> wrote:

  
    
#
Joshua Wiley <jwiley.psych <at> gmail.com> writes:

  [snip]
or test[[first]] ; you probably do not want test[first] , which returns
a list of length 1 containing the column of interest, rather than the
column itself ...