2

This question may seem stupid, but I am curious about how to access a column by a column name with quote in data.frame, such as:

col.name <- colnames(feature)[i]   # feature is a data frame, i is the index
feature$col.name   # but this returns NULL
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
chashaobao
  • 23
  • 1
  • 2
  • 10
  • 5
    Use extractor `[[`: `feature[[col.name]]` – Parfait Apr 10 '21 at 23:28
  • 1
    this is definitely a FAQ, but it's hard to search for `$` (in fact I can't find the appropriate duplicate even after searching for a few minutes ...) – Ben Bolker Apr 10 '21 at 23:37
  • 2
    https://stackoverflow.com/q/1169456/3358272, https://stackoverflow.com/q/33462844/3358272, https://stackoverflow.com/q/10875511/3358272. Google is better at it than Stack, [`site:stackoverflow.com "r" "$" "[["`](https://www.google.com/search?q=site%3Astackoverflow.com+"r"+"%24"+"[["). – r2evans Apr 10 '21 at 23:39
  • 2
    @r2evans these are mostly about "difference between [ and [[", not "how do index indirectly with $" (@user20650's suggestion works). I think I'm going to tag that one with [r-faq] so I can find it again ... – Ben Bolker Apr 10 '21 at 23:42
  • Yeah, @BenBolker, thank you for checking me on that (one reason I didn't close the question quickly): I'm too distracted at the moment to do an earnest look at all of them, those looked somewhat promising. – r2evans Apr 10 '21 at 23:49
  • Thanks! I searched but failed to find the answer as dollar sign is not accounted for using Google – chashaobao Apr 10 '21 at 23:51

0 Answers0