0

I was wondering why Example 2 in the following doesn't work (and potential solutions of course :P)

test <- data.frame('a'=1:5,'b'=6:10)
#Example 1:
'$'(get('test'),'b')

#Example 2:
columnname = 'b'
'$'(get('test'),columnname)
user2792957
  • 319
  • 2
  • 5
  • 3
    You will head off a _lot_ of commentary here if you take some time to explain in your question _why_ you're trying to do this, probably acknowledging that this is mostly just to understand `$`, not that you're actually trying to use `$` this way. – joran Oct 10 '13 at 20:11
  • 1
    @joran Maybe just as an exercise, like my [maybe-related question](http://stackoverflow.com/questions/18216084/lapply-ing-with-the-function) – Frank Oct 10 '13 at 20:12
  • 1
    Try `test[[columname]]`, and if that answers your real question, just indicate that. – Josh O'Brien Oct 10 '13 at 20:13
  • 1
    @Frank I would hope so, but if the OP doesn't explain further, they are likely to get lots and lots of responses along the lines of "just use `[[`", which probably isn't what they want. – joran Oct 10 '13 at 20:13
  • @joran -- I'm guessing that it is (since they ask for potential solutions). To understand *why* `$` does this, have a look at: `test$b` and `test$"b"`. – Josh O'Brien Oct 10 '13 at 20:14
  • The reason why I'm doing this instead of 'test$...' is because I am building a function that requires references (in loops) to column names in multiple tables where it is not possible to reference by numeric indices. – user2792957 Oct 10 '13 at 20:16
  • @JoshO'Brien, I think they would want `get("test")[[columnname]]` – Ricardo Saporta Oct 10 '13 at 20:17
  • @user2792957, If that's your goal, then your best solution is as Josh suggested – Ricardo Saporta Oct 10 '13 at 20:17
  • @JoshO'Brien I stand corrected. I never imagined that someone would get this far down the rabbit hole with `$` without investigating other options. – joran Oct 10 '13 at 20:18
  • Gah, didn't know you could reference like that! test[[columnname]] was what I was looking for...sorry @joran! :) – user2792957 Oct 10 '13 at 20:18
  • No worries! :) FYI the somewhat non-obvious help page for these things is at `?Extract`. – joran Oct 10 '13 at 20:21
  • I've [**said it before**](http://stackoverflow.com/a/18228613/1478381), I'll say it again. `$`'s arguments are *never* evaluated. – Simon O'Hanlon Oct 10 '13 at 20:26

2 Answers2

3

BECAUSE $ DOES NOT EVALUATE ITS SECOND ARGUMENT. (I thought this fact was clearly stated in the help page but after reviewing it, I am not so sure I can make that claim.) The relevant sentence in the help page is:

The main difference is that $ does not allow computed indices, whereas [[ does.

As iTech suggests, a crucial difference between [[ and $ is that $ requires a character vector as its second argument, while [[ is able to accept a character vector or an object name to be evaluated. (Another difference is that $ only accepts recursive object for its first argument.)

In searching for the prior times this question has been addressed I discovered another solution that should only be used to learn the language and not for your coding strategies:

columnname = 'b'
do.call(  '$', list(get('test'), columnname))
[1]  6  7  8  9 10

This does provide another mechanism for evaluating the second argument as well as the first, but it looks like tortured code to me. You should be gathering your items to be processed in a list and running through them with lapply or sapply using "[[" or "[" as appropriate arguments for FUN.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • And the source says... "*We need to be sure to only evaluate the first argument. The second will be a symbol that needs to be matched, not evaluated.*". – Simon O'Hanlon Oct 10 '13 at 21:06
  • Another duplicate which I had forgotten: http://stackoverflow.com/questions/10546826/r-query-on/10550427#10550427 – IRTFM Oct 10 '13 at 21:20
1

Just change $ to [[

Here is an example:

'[['(get('test'),columnname)

The idea is by using $ R is expecting the index to be a direct value or a symbol that does not require evaluation i.e. not a variable

iTech
  • 18,192
  • 4
  • 57
  • 80