简体   繁体   中英

R - Need to subset a data frame using matches from a regex expression

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.

new.data.frame <- old.data.frame[grep("word1", data$Column2, perl=TRUE), ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM