What is an alternative to passing a variable to a regular expression in R?

My snippet of code to remove short and long words from some text:

# Remove Words based on lowerCutOff & upperCutOff removeByLength<- function(text,lowerCutOff=2,upperCutOff=12){ text<- gsub("\\b[a-zA-Z0-9]{1,lowerCutOff}\\b|\\b[a-zA-Z0-9]{upperCutOff,}\\b"," ",text) return(text) } 

How can I achieve the necessary functionality without hard coding the lower and upper cut-offs?

+4
source share
1 answer

Use paste to concatenate lines to create a pattern:

 removeByLength<- function(text,lowerCutOff=2,upperCutOff=12){ pattern <- paste("\\b[a-zA-Z0-9]{1,",lowerCutOff, "}\\b|\\b[a-zA-Z0-9]{",upperCutOff,",}\\b", sep="") text <- gsub(pattern, " ", text) return(text) } 
+4
source

All Articles