2

I have a vector 'numeric_column_names' that contains the data below:

print(numeric_column_names)
 [1] "an_id"             "numeric_col_1"
 [3] "numeric_col_2"     "numeric_col_3"
 [5] "another_id"        "numeric_col_4"

How do I remove all of the elements that have the text "_id" so the output would be:

print(numeric_column_names)
 [1] "numeric_col_1"     "numeric_col_2"
 [3] "numeric_col_3"     "numeric_col_4"
Jason Melo Hall
  • 652
  • 2
  • 11
  • 23
  • 4
    `numeric_column_names[-grep("_id", numeric_column_names)]` – Gregor Thomas Jan 24 '17 at 21:32
  • A more canonical dupe is [Test if characters in string](http://stackoverflow.com/q/10128617/903061). The `grepl` solution would be the same but with `!` instead of `-` in the brackets. – Gregor Thomas Jan 24 '17 at 21:35
  • I would argue that it is similar but different in the fact that it incorporates a vector. Similar questions were asked with dataframes too which was not duped, and I wasn't able to figure it out from that context. For a someone who is new to a language this granularity is helpful. Thank you for the answer though. If you would like credit please post an answer. – Jason Melo Hall Jan 24 '17 at 21:45
  • 1
    I mean, it's not *identical*. Technically yours has two steps, (1) finding which elements contain `"_id"` and (2) removing those from the vector. Presumably (1) is the part you were stuck on. And R doesn't have "atomics" - any string is a vector, even if it is just a 1-element vector. The same code will work. – Gregor Thomas Jan 24 '17 at 21:59
  • 3
    As for credit, I don't care much about a few points of reputation; I have plenty. I'd much rather have a pointer to an old question with many useful answers than another variation of the same thing. I only wish I had marked [this one as the dupe](http://stackoverflow.com/questions/10128617/test-if-characters-in-string-in-r) rather than the first one I found. Hopefully people who miss finding that one directly will find this one and follow the link there. – Gregor Thomas Jan 24 '17 at 22:00

0 Answers0