Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex R中具有子集/grepl的查找表_Regex_R_Dplyr - Fatal编程技术网

Regex R中具有子集/grepl的查找表

Regex R中具有子集/grepl的查找表,regex,r,dplyr,Regex,R,Dplyr,我正在分析一组使用爬虫提取的URL和值。虽然我可以从URL中提取子字符串,但我真的不想麻烦使用正则表达式这样做。有没有一种简单的方法可以使用subset/grepl进行查找表样式替换,而不必求助于dplyr(对变量进行条件变异) 我目前的进程: test <- data.frame( url = c('google.com/testing/duck', 'google.com/evaluating/dog', 'google.com/analyzing/cat'), content

我正在分析一组使用爬虫提取的URL和值。虽然我可以从URL中提取子字符串,但我真的不想麻烦使用正则表达式这样做。有没有一种简单的方法可以使用subset/grepl进行查找表样式替换,而不必求助于dplyr(对变量进行条件变异)

我目前的进程:

test <- data.frame(
  url = c('google.com/testing/duck', 'google.com/evaluating/dog', 'google.com/analyzing/cat'),
  content = c(1, 2, 3),
  subdir = NA
)

test[grepl('testing', test$url), ]$subdir <- 'testing'
test[grepl('evaluating', test$url), ]$subdir <- 'evaluating'
test[grepl('analyzing', test$url), ]$subdir <- 'analyzing'
for(c语言中的目标('testing'、'evaluation'、'analysis')){
test[grepl(target,test$url),'subdir']
for(c中的目标('testing','evaluation','analysis')){
test[grepl(target,test$url),'subdir']试试这个:

test$subdir<-gsub('.*\\/(.*)\\/.*','\\1',test$url)
test$subdir试试这个:

test$subdir<-gsub('.*\\/(.*)\\/.*','\\1',test$url)

test$subdir不理解您的目标。您能详细说明吗?使用
sub
提取值可能比使用嵌套的
ifelse
更容易。即
gsub(“^[^/]+\/\\\/\\/.$”,test$url)
与第一个解决方案相比,您的第二个解决方案分配了不同的值。我不认为嵌套的ifelse更容易与我一起工作。我不理解您的目标。请您详细说明一下。使用
sub
提取值可能比使用嵌套的
ifelse
更容易。例如
gsub('^[^/]+\\/|\\/.*$','',测试$url)
与第一个解决方案相比,您的第二个解决方案分配了不同的值。我不认为嵌套的ifelse更容易与我一起工作。好主意。但是,它会为那些没有matchNice想法的项目提供整个test$url。但是,它会为那些没有matchA想法的项目提供整个test$urlh、 是的。应该考虑在目标和结果列表上循环/递归。如果目标与替换不同,您可以使用
for()
mapply()
,或
sapply()中的一个循环它们的索引
这是一个非常好的解决方案。想不出任何更好的方法。啊,是的。应该考虑在目标和结果列表上循环/递归。如果目标与替换不同,您可以使用
for()
mapply()
sapply()中的一个循环它们的索引
这是一个非常好的解决方案。想不出更好的办法了。
 for (target in  c('testing','evaluating','analyzing') ) {
                    test[grepl(target, test$url),'subdir' ] <- target }

 test
                        url content     subdir
1   google.com/testing/duck       1    testing
2 google.com/evaluating/dog       2 evaluating
3  google.com/analyzing/cat       3  analyzing
targets <-   c('testing','evaluating','analyzing') 
for( target in targets ) { ...}
test$subdir<-gsub('.*\\/(.*)\\/.*','\\1',test$url)