使用变量在 R 中创建正则表达式模式 [英] Using variable to create regular expression pattern in R

查看:33
本文介绍了使用变量在 R 中创建正则表达式模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个功能:

ncount <- function(num = NULL) {

 toRead <- readLines("abc.txt")
 n <- as.character(num)
 x <- grep("{"n"} number",toRead,value=TRUE)

}

grep-ing 时,我希望函数中传递的 num 动态创建要搜索的模式?这如何在 R 中完成?文本文件每行都有数字和文本

While grep-ing, I want the num passed in the function to dynamically create the pattern to be searched? How can this be done in R? The text file has number and text in every line

推荐答案

您可以使用 paste 来连接字符串:

You could use paste to concatenate strings:

grep(paste("{", n, "} number", sep = ""),homicides,value=TRUE)

这篇关于使用变量在 R 中创建正则表达式模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆