Mark - Since regular expressions in R are just character strings, it's pretty easy to assemble a regular expression to delete leading or trailing characters. For example:
delchars = function(str,n,lead=TRUE){
+ dots = paste(rep('.',n),collapse='') + pat = if(lead)paste('^',dots,sep='') else paste(dots,'$',sep='') + sub(pat,'',str) + }
str = "this is a test" delchars(str,4)
[1] " is a test"
delchars(str,4,lead=FALSE)
[1] "this is a " - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spec...@stat.berkeley.edu On Tue, 14 Dec 2010, Mark Na wrote:
Hi R-helpers, I have a character string, for example: "lm(y ~ X2 + X3 + X4)" from which I would like to strip off the leading and trailing quotation marks resulting in this: lm(y ~ X2 + X3 + X4) I have tried using gsub() but I can't figure out how to specify the quotation mark using a regular expression. Alternatively, I would like a function that lets me delete the leading (or trailing) X characters, and in this case X=1 (but it could be used more flexibly to delete several leading or trailing characters). I would appreciate help with either of these potential solutions (gsub and regex, or delete leading/trailing characters). Many thanks! Mark ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.