Regex 使用grep函数保留特定行
我有一个大的数据集,变量包含不同的格式Regex 使用grep函数保留特定行,regex,replace,Regex,Replace,我有一个大的数据集,变量包含不同的格式 Subject Result 1 3 2 4 3 <4 4 <3 5 I need to go to school<> 6 I need to <> be there 7 2.3 need to be< there 8 <.3 9 .<
Subject Result
1 3
2 4
3 <4
4 <3
5 I need to go to school<>
6 I need to <> be there
7 2.3 need to be< there
8 <.3
9 .<9
10 ..<9
11 >3 need to go to school
12 <16.1
13 <5.0
<代码>主题结果
1 3
2 4
3您只需应用两个grep,一个用于查找“”键,另一个用于删除带有字符的字段:
grep“[>假设您确定[,:;]是唯一有问题的标点符号:
df$Result<-gsub("[.,;:]","", df$Result) # remove any cases of [.,;:] from your results column
df[grep("^\\s*[<>][0-9]+$", df$Result),] # find all cases of numbers preceded by < or > (with possible spaces) and succeeded by nothing else.
df$Result(带有可能的空格),并且没有任何其他结果成功。
非常感谢。我无法运行代码。请运行示例上的代码并发布完整的代码。有两种方法,如果它位于名为“file.txt”的文件中,请运行类似以下cat file.txt的命令| grep”[>我运行一个你所在的环境?我从bash shell的角度来讨论它。如果你可以从你的环境运行bash命令,那么我的代码将很有帮助。我猜你在R中。试试这个:当询问regex(或者其他什么,真的)时,AIt很重要明确说明您使用的语言——最好还包括一个标记。
df$Result<-gsub("[.,;:]","", df$Result) # remove any cases of [.,;:] from your results column
df[grep("^\\s*[<>][0-9]+$", df$Result),] # find all cases of numbers preceded by < or > (with possible spaces) and succeeded by nothing else.