简体   繁体   English

R - 需要使用正则表达式中的匹配来对数据帧进行子集化

[英]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. 我希望基于来自扫描单个列的正则表达式的匹配来对数据帧进行子集化,并返回列2与正则表达式匹配的所有行中的数据。

Using R 3.01 and I'm a relative inexperienced R programmer. 使用R 3.01,我是一个相对缺乏经验的R程序员。

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: 我正在使用以下内容扫描第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. 到目前为止,我得到了从column2返回的包含word1所有字符串,但我希望将找到这些匹配的整行进行子集化。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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