
Thank you so much for the help. 

 

Is the idea behind doing this to convert the text file to a dataframe to allow me to work with and manipulate it?  Also, if I do convert it to a dataframe how do I reconvert it to a text file so that I can run it as a properly formatted input file in the water quality model?

 

Also, I should have included more of the input file and explained a little more. Below is a longer version of the input file just to give you an idea of what different parts of it may look like.  

 

Е.

 Inorganic Solids 2 (mg-DW/L) r
    2               Number of boundaries
      1.000000      1.000000  Boundary Scale & Conversion Factors
    1               Boundary Segment Number
    2               Number of time-concentration values
   0.000000  0.000000
   75.0000  0.000000
    5               Boundary Segment Number
    2               Number of time-concentration values
   0.000000  0.000000
   75.0000  0.000000
Е.

Е.

   1               Segment number
   2  26.3900
   4  2.00000
   6  7.00000
   7  2.00000
   8  1.50000
    2               Segment number
   2  26.3900
   4  2.00000
   6  7.00000
   7  2.00000
   8  1.50000

Е.
Е.
2.50000 Phytoplankton Maximum Growth Rate Constant @20 °C for Group 1 (1/day)
1.08000 Phytoplankton Growth Temperature Coefficient for Group 1
0.200000 Phytoplankton Respiration Rate Constant @20 °C for Group 1 (1/day)
1.04500 Phytoplankton Respiration Temperature Coefficient for Group 1
3.000000E-02 Phytoplankton Death Rate Constant (Non-Zoo Predation) for Group 1 (1/day)
0.000000 Phytoplankton Zooplankton Grazing Rate Constant for Group 1 (1/day)
2.500000E-02 Phytoplankton Half-Saturation Constant for N Uptake for Group 1 (mg N/L)
Е. 
 
 
I am going to have to change different values throughout the file, not just those numbers at the beginning of that last snippet. For example, I may want to change what is in the second row under "Segment number". To do this could I do something like:
 
# L <- readLines("myfile")
 
out <- strapply(L, "(.*)",
~ data.frame(text, stringsAsFactors = FALSE),
simplify = rbind)
 
Would this then give me each row in a data frame that I could then go in and work with?
 
How would I then go in and do steps 1(b) and 1(c)ЕЕ (1) (b)find the lines in the input file to insert parameter values into(values used in DEOptim optimization between УlowerФ and УupperФ), (c) insert those parameter values into the input file in the proper spot, and then run this altered input file(step 2)ЕЕ Per 1(b) how do I tell the function to insert the parameter values selected in the DEOptim routine into the input file so that they can be run in the model? Also, would using conditional logic or maybe subset() be a way to start approaching the finding and replacing of lines in the dataframe?
 
Again, thank you for your help. Any additional help is very much appreciated.

 
> From: ggrothendieck@gmail.com
> Date: Sun, 25 Apr 2010 12:35:26 -0400
> Subject: Re: [R] Manipulating text files
> To: leavealetter1@hotmail.com
> CC: r-help@r-project.org
> 
> Try this. First we read in the lines using readLines. (We use
> textConnection here to keep it self contained but you can read it from
> the file as shown in the commented out portion.)
> 
> Using strapply we match the regular expression to the input. The two
> parenthesized portions match the number (\\S+ means one or more
> non-space characters), we then match a space and the rest (.* means
> anything). These are processed as the two arguments of the function
> given in formula notation. Since the arguments are not explicitly
> listed it is assumed that the free variables no and text are the
> arguments. The result is a data frame with a numeric column, no,
> holding the number and a text column, text, holding the text.
> 
> 
> Lines <- "2.50000 Phytoplankton Maximum Growth Rate Constant @20 °C
> for Group 1 (1/day)
> 1.08000 Phytoplankton Growth Temperature Coefficient for Group 1
> 0.200000 Phytoplankton Respiration Rate Constant @20 °C for Group 1 (1/day)
> 1.04500 Phytoplankton Respiration Temperature Coefficient for Group 1
> 3.000000E-02 Phytoplankton Death Rate Constant (Non-Zoo Predation) for
> Group 1 (1/day)
> 0.000000 Phytoplankton Zooplankton Grazing Rate Constant for Group 1 (1/day)
> 2.500000E-02 Phytoplankton Half-Saturation Constant for N Uptake for
> Group 1 (mg N/L)"
> 
> library(gsubfn) # http://gsubfn.googlecode.com
> 
> # L <- readLines("myfile")
> L <- readLines(textConnection(Lines))
> 
> out <- strapply(L, "(\\S+) (.*)",
> ~ data.frame(no = as.numeric(no), text, stringsAsFactors = FALSE),
> simplify = rbind)
> 
> An alternative to the last statement using only sub is:
> 
> out <- data.frame(no = as.numeric(sub(" .*", "", L)),
> text = sub("^[^ ]+ ", "", L))
> 
> Either of the above out <- statements produces:
> 
> > out
> no text
> [1,] 2.5 "Phytoplankton Maximum Growth Rate Constant @20 °C for
> Group 1 (1/day)"
> [2,] 1.08 "Phytoplankton Growth Temperature Coefficient for Group 1"
> [3,] 0.2 "Phytoplankton Respiration Rate Constant @20 °C for Group 1
> (1/day)"
> [4,] 1.045 "Phytoplankton Respiration Temperature Coefficient for
> Group 1"
> [5,] 0.03 "Phytoplankton Death Rate Constant (Non-Zoo Predation) for
> Group 1 (1/day)"
> [6,] 0 "Phytoplankton Zooplankton Grazing Rate Constant for Group
> 1 (1/day)"
> [7,] 0.025 "Phytoplankton Half-Saturation Constant for N Uptake for
> Group 1 (mg N/L)"
> 
> 
> On Sun, Apr 25, 2010 at 12:09 PM, galen kaufman
> <leavealetter1@hotmail.com> wrote:
> >
> > Dear R Community,
> >
> > I am trying to optimize a water quality model that I am using.  Based on conversations with others more familiar with what I am doing I plan to implement DEOptim to do this. The water quality model is interfaced through a GUI. I have the input file necessary to alter parameters and run the model as a text file.
> >
> > To do the optimization I have figured out the general procedure but I need some help on the specific methods and commands that may be helpful.
> >
> > Here is an example of a section of the input file:
> >
> > Е.
> > 2.50000 Phytoplankton Maximum Growth Rate Constant @20 °C for Group 1 (1/day)
> > 1.08000 Phytoplankton Growth Temperature Coefficient for Group 1
> > 0.200000 Phytoplankton Respiration Rate Constant @20 °C for Group 1 (1/day)
> > 1.04500 Phytoplankton Respiration Temperature Coefficient for Group 1
> > 3.000000E-02 Phytoplankton Death Rate Constant (Non-Zoo Predation) for Group 1 (1/day)
> > 0.000000 Phytoplankton Zooplankton Grazing Rate Constant for Group 1 (1/day)
> > 2.500000E-02 Phytoplankton Half-Saturation Constant for N Uptake for Group 1 (mg N/L)
> > Е.
> >
> > The first numbers of each line are the parameter values that I am interested in.
> >
> > I have familiarized myself with DEOptim but need more guidance on writing the function to that will run the water quality model and feed DEOptim.
> >
> > In general I need to write a function to: (1) open and write over the model input file, inserting parameter values from DEOptim to the specific lines where they belong in the input file, (2) run the input file in the water quality model, (3) read the output files, (4) select values of interest in the output, (5) use values of interest in the equation to be optimized, then the result of the equation will be what is minimized in DEOptim. Steps 2-5 I am reasonably familiar with. I am not as familiar with how I would work with what I describe below for step (1).
> >
> > (1) (a)open the input file, (b)find the lines in the input file to insert parameter values into(values used in DEOptim optimization between УlowerФ and УupperФ), (c) insert those parameter values into the input file in the proper spot, and then run this altered input file(step 2). Is this the right way to start?
> >
> > #function
> > optimfxn<- function (x) {
> >
> > #(1)(a) would I do something like this?
> >
> > txt<-readLines("inp_File.inp")
> >
> > #From here I need help finishing step 1. I have not worked much with text files in R. I have read a lot of help files but am not getting anywhere.
> >
> > #Do I have to use textConnection() to write to the text file?
> >
> > #How do I incorporate the parameter values selected in each iteration of DEOptim into the input file?
> >
> > Here is an example of what I would be running in DEOptim:
> >
> > optimization<-DEoptim( fn=optimfxn, lower=c(phyt_g_rt_coef=1.01000, phyt_mx_g_rt=0.98000),upper=c(phyt_g_rt_coef=1.20000, phyt_mx_g_rt=4.44000)
> >
> > Any help you could provide would be very much appreciated.  Thank you in advance.
> >
> > Galen
> > _________________________________________________________________
> > Hotmail has tools for the New Busy. Search, chat and e-mail from your inbox.
> >
> > N:WL:en-US:WM_HMP:042010_1
> >        [[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.
> >
> >
 		 	   		  
_________________________________________________________________
The New Busy think 9 to 5 is a cute idea. Combine multiple calendars with Hotmail. 

PID28326::T:WLMTAGL:ON:WL:en-US:WM_HMP:042010_5
	[[alternative HTML version deleted]]

