Reputation: 182
I'm looking to subset a data frame based on matches from a regular expression that scans a single column, and returns the data in all the rows where column 2 has a match from the regular expression.
Using R 3.01 and I'm a relative inexperienced R programmer.
My data frame looks like this:
data:
........Column 1 .. Column2 Column 3
Row 1 ..data..........string....data
Row 2 ..data..........string....data
Row 3 ..data..........string....data
Row 4 ..data..........string....data
I'm using the following to scan column 2:
grep("word1", data$Column2, perl=TRUE)]
So far, I get all the strings returned from column2 that contain word1
, but I'm looking to subset the entire row(s) where those matches are found.
Upvotes: 5
Views: 7374
Reputation: 6047
new.data.frame <- old.data.frame[grep("word1", data$Column2, perl=TRUE), ]
Upvotes: 5