20

From the reading I've been doing with R, I can select a column in a data frame by either of these two methods: frame[,column] or frame$column. However, when I have a string as a variable, it works only in the first. In other words, consider the following:

I have a data frame, tmp, a subset of a larger data frame of question responses. V1 is the responder's id, Q5.3 is the response, a 1 or 0:

            V1 Q5.3
2 R_bdyKkzWcvBxDFTT    1
3 R_41wnKUQcM8mUW2x    0
4 R_2ogeykkgbH2e4RL    1
5 R_8D4jzMBfYO0M0ux    1
6 R_3KPgP2pxWROnip7    1

str(tmp)

'data.frame':   5 obs. of  2 variables:
     $ V1  : Factor w/ 364 levels "R_0039orNoOoWaDQx",..: 256 116 70 201 95
     $ Q5.3: num  1 0 1 1 1

Now, I define a variable x, that holds the string of the name of one of the columns.

x<-"Q5.3"

tmp[,x] returns what I think it should return:

tmp[,x]

[1] 1 0 1 1 1

tmp$"Q5.3" returns what I think it should return:

tmp$"Q5.3"

[1] 1 0 1 1 1

tmp$x however returns

tmp$x

NULL

How can I tell R to interpret tmp$x as tmp$"Q5.3".

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
William Oliver
  • 361
  • 1
  • 2
  • 11

1 Answers1

33

If you have a variable x with a column name in tmp, tmp[,x] or tmp[[x]] are the correct ways to extract it. You cannot get R to treat tmp$x as tmp$"Q5.3". tmp$x will always refer to the item named "x" in "tmp".

NelsonGon
  • 13,015
  • 7
  • 27
  • 57
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • this has got to be a dupe ... – Ben Bolker May 18 '14 at 00:34
  • @BenBolker I can never find a good dup for this question even though it comes up soooooo often. I was thinking of starting one. "What's the difference between $, [], and [[]] when working with data.frames?" – MrFlick May 18 '14 at 00:35
  • Do you know where this is discussed in any documentation? I couldn't find it anywhere, and have wasted a bit of time trying things like parse() and such... – William Oliver May 18 '14 at 00:37
  • http://stackoverflow.com/questions/6242598/accessing-r-list-elements-through-function-parameters – Ben Bolker May 18 '14 at 00:39
  • @WilliamOliver it's talked about in the ?"$" help page but it's not super clear i will admit. If you search for "One common mistake" on this page http://adv-r.had.co.nz/Subsetting.html you will get an overview of the issue. – MrFlick May 18 '14 at 00:40
  • 1
    Thx. I'm not surprised that browsing the questions didn't turn it up. – William Oliver May 18 '14 at 00:40
  • @MrFlick -- That's a great page. Thanks. – William Oliver May 18 '14 at 00:41
  • @BenBolker That's not bad, but it's certainly not as direct as I would like it to be. I can't blame others for not understanding what's going on there. I'd like something more like the link i shared above. – MrFlick May 18 '14 at 00:41
  • Not related to this but canonical for `[` vs. `[[`: http://stackoverflow.com/questions/1169456/in-r-what-is-the-difference-between-the-and-notations-for-accessing-the/1169495#1169495 – Ben Bolker May 18 '14 at 00:42
  • I'm sure there are more out there, but `$` is hard to search for. – Ben Bolker May 18 '14 at 00:43
  • 1
    @BenBolker Here are a couple, http://stackoverflow.com/questions/19730806/r-access-data-frame-column-using-variable, http://stackoverflow.com/questions/18222286/select-a-specific-column-using-the-dollar-sign-in-r. The latter seems to have more closed questions pointing to it. – GSee May 18 '14 at 00:50
  • @GSee Thanks for those pointers, the explanation at http://stackoverflow.com/questions/18222286/select-a-specific-column-using-the-dollar-sign-in-r was great – William Oliver May 18 '14 at 19:51