Friday, 15 April 2011

Overwrite variables to a specific position in text file with R -


I am trying to "replace" the variable in an existing text file with my R script (one part below ). Variables are called var1 to var6 and the values ​​below need to be changed again. The status of the variable in the text file is always the same

  *** Block B:. Water flow information ******************************************* hTab1 hTabN 1e-006 10000 MH var1 var2 Var3 var4 var5 var6 0.095 0.41 0.03 1.5 0.26 0.5   

Has anyone got any ideas? Kind regards

I get this solution, maybe someone is more simple.

  1. To get a lisst of lines using readlines
  2. use grepl line contaings var1 < / Li>
  3. Using the extract values ​​in the vector strsplit : xx
  4. Creating a new list of vector, with new values ​​and using this name xx: here New values ​​are 1: 6
  5. Use gsubfn to replace the old list

    My code here

      ll & lt; -. ReadLines (textConnection (object = '** * block b: water flow info *********************************************** ** hTab1 hTabN 1e-006 10000 MH 0 var1 var2 var3 var4 var5 var6 0,095 0,41 0,03 1,5 0,26 0,5 ')) ## Get values ​​id & lt line - which (grepl ('Var1', ll)) ## xx & lt; -strsplit (gsub ('[+] +', '|', ll [id + 1]), '[| |') ## Prices instead of library (gsubfn) Vals & lt; - 1: 6 Name (Vals) & lt; - xx [[1]] Ll [id + 1] & lt; - Gsubfn ("[[Issue:]] + [.] [[Issue:]] +", as.list (Vals), ll [ID + 1] 1] "*** Block B: Water flow information * **************************************** "[2]" hTab1 hTabN "[3]" 1e-006 10000 "[4]" MH "[5]" 0 "[6]" var1 var2 var3 var4 var5 var6 "[7]" 1 2 3 4 5 6 "   < / Html>

No comments:

Post a Comment