24

I would like to choose rows based on the subsets of their names, for example

If I have the following data:

data <- structure(c(91, 92, 108, 104, 87, 91, 91, 97, 81, 98), 
.Names = c("fee-", "fi", "fo-", "fum-", "foo-", "foo1234-", "123foo-", 
"fum-", "fum-", "fum-"))

how do I select the rows matching 'foo'?

using grep() doesn't work:

 grep('foo', data)

returns:

integer(0)

what am I doing wrong? or, is there a better way?

Thanks!

oguz ismail
  • 1
  • 16
  • 47
  • 69
David LeBauer
  • 31,011
  • 31
  • 115
  • 189

3 Answers3

27

You need to grep the names property of data, not the values property.

For your example, use

> grep("foo",names(data))
[1] 5 6 7
> data[grep("foo",names(data))]
  foo- foo1234-  123foo- 
  87       91       91 

One other clean way to do this is using data frames.

> data <- data.frame(values=c(91, 92, 108, 104, 87, 91, 91, 97, 81, 98), 
                   names = c("fee-", "fi", "fo-", "fum-", "foo-", "foo1234-", "123foo-", 
                   "fum-", "fum-", "fum-"))

> data$values[grep("foo",data$names)]
[1] 87 91 91
Alex Brown
  • 41,819
  • 10
  • 94
  • 108
6

Use subset in combination with regular expressions:

subset(your_data, regexpr("foo", your_data$your_column_to_match) > 0))

If you just care about a dataset with one column I guess you do not need to specify a column name...

Philip

Philip
  • 3,470
  • 7
  • 29
  • 42
  • 6
    I usually find `grepl` to be more useful here -- you can skip the comparison vs 0 thing, which makes the code look a little cleaner. – Harlan Nov 19 '10 at 20:07
2
> grep("foo",names(data), value=T)
[1] "foo-"     "foo1234-" "123foo-" 

if value is true, it returns the content instead of the index

user1836270
  • 71
  • 1
  • 2