Re: [R] How to left or right truncate a character string?

2010-12-14 Thread Henrique Dallazuanna
Try this:

noquote(lm(y ~ X2 + X3 + X4))

To remove X characters:

gsub(^.|.$, , lm(y ~ X2 + X3 + X4))


On Tue, Dec 14, 2010 at 6:27 PM, Mark Na mtb...@gmail.com 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.




-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40 S 49° 16' 22 O

[[alternative HTML version deleted]]

__
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.


Re: [R] How to left or right truncate a character string?

2010-12-14 Thread Phil Spector

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.