Reg表达式子集单独工作,但在函数中没有任何反应

时间:2015-11-14 04:03:40

标签: regex r function

晚上好,

我有一个列表“a”,我使用正则表达式成功进行了子集。

a=a[grep("Macy*|Nors*", a$Geography, perl=TRUE),]
a=a[grep("Levis*|Diesel*|Replay*", a$Brand.Name, perl=TRUE), ]
a=a[grep("Week*", a$Time, perl=TRUE), ]

我在下面创建了函数clean但是当我将它应用到我的列表时“a”没有任何反应

clean=function(x){
x=x[grep("Macy*|Nors*", x$Geography, perl=TRUE),]
x=x[grep("Levis*|Diesel*|Replay*", x$Brand.Name, perl=TRUE), ]
x=x[grep("Week*", x$Time, perl=TRUE), ]
return (x)    
}

清洁(a)只返回原始“a” 我尝试打印每个步骤但实际上没有任何反应。 谢谢你的帮助

1 个答案:

答案 0 :(得分:0)

a = data.frame(Geography = c(paste('Macy',1:5, sep = " "),'john'),stringsAsFactors = FALSE)

a

      Geography
1    Macy 1
2    Macy 2
3    Macy 3
4    Macy 4
5    Macy 5
6      john

clean = function(x){
x = x[grep("Macy*|Nors*", x[['Geography']], perl = TRUE),]
return(x)    
}

clean(a)
[1] "Macy 1" "Macy 2" "Macy 3" "Macy 4" "Macy 5"