[R] Modify string-regular expression

2008-05-30 Thread Romain
Hi there everybody,

I would like to be able to modify a string like 'Param_i = Val_i-1' in 'Param_i 
= Val_i-2' in a text file
Indeed I have two arrays :
-One for the name of my parameters
-One for the new values for each parameter

I would like to write a loop like this one :

...

SCAN <- scan("File.txt",sep="\n",  what="raw",blank.lines.skip=F)
For (i in 1:Nb_param)
{
sub('Param[i] = Value_i-1','Param[i] = Value_i-2', SCAN)
}

...

I Know how to modify a string with sub when it is a fixed string : sub("(K 
=)([0-9]*)",paste("\\1", Value[i,2]),SCAN)
But i would like to know if it is possible to use the function paste or 
something else in the first argument of the function sub.
For example, the correct syntax of :
' sub("(Param[i])([0-9]*)",paste("\\1", Value[i,2]),SCAN) '


If I'm not clear enough please let me know..
Thank you for your help.


Romain










Vous aussi bénéficiez d'1 Go de stockage gratuit en ligne avec Voila  
http://macle.voila.fr

__
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] Modify string-regular expression

2008-05-30 Thread Hans-Joerg Bibiko

On 30 May 2008, at 11:25, Romain wrote:

...

SCAN <- scan("File.txt",sep="\n",  what="raw",blank.lines.skip=F)
For (i in 1:Nb_param)
{
   sub('Param[i] = Value_i-1','Param[i] = Value_i-2', SCAN)
}

...

I Know how to modify a string with sub when it is a fixed string :  
sub("(K =)([0-9]*)",paste("\\1", Value[i,2]),SCAN)
But i would like to know if it is possible to use the function paste  
or something else in the first argument of the function sub.

For example, the correct syntax of :
' sub("(Param[i])([0-9]*)",paste("\\1", Value[i,2]),SCAN) '


Have a look at ?gsub

Cheers,

--Hans

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