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) }
source share