3

I have a data frame called LeaseDF. I am looking to pull all observations where the Team_Code column is contains the letter "t". The simple code I have is below. Somehow is not returning anything. I have also tried for loops with the grepl function and lapply with grepl to no avail. Thanks.

subset <- LeaseDF[grep("^t-", LeaseDF$TEAM_CODE),]
1
  • 2
    Why did you use the regular expression "^t-" if all you want to check is occurrence of the letter “t”? Commented Jun 16, 2016 at 21:34

1 Answer 1

11

I assume that with "pull" you mean subset?

As you didn't add your data I am giving you my example, where I used package sqldf

df <- data.frame(name = c('monday','tuesday','wednesday', 'thursday', 'friday'))
require(sqldf)
# Select specific values from a column i.e., containing letter "t"
sqldf("select * from df where name LIKE '%t%'")
# And output
     name
1  tuesday
2 thursday

Or use grep

df$name[grep("t", df$name) ]
# And output
[1] tuesday  thursday
Levels: friday monday thursday tuesday wednesday

# OR use ^t if you want beginning of the string
df[grep("^t", df$name), ] 

Or use grepl and you could also exclude non-matching observations

df[grepl("t", df$name), , drop = FALSE]
# Output
      name
2  tuesday
4 thursday
2
  • Yes I mean subset. Ok this makes sense. Thanks! I ended up using the sqldf package myself. But I was looking for a way to do it without that package.
    – Lyle
    Commented Jun 20, 2016 at 16:00
  • I am glad to help.
    – Miha
    Commented Jun 20, 2016 at 18:24

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.