One possibility involves applying a regular expression via gsub:

   as.numeric(gsub("^\\[1\\] ","",fvals))


On Friday 24 June 2011 14:39:31 Ravi Varadhan wrote:
> Hi,
> 
> I would like to know how to capture the console output from running an 
> algorithm for further analysis.  I can capture this using capture.output() 
> but 
that yields a character vector.  I would like to extract the actual numeric 
values.  Here is an example of what I am trying to do.
> 
> fr <- function(x) {   ## Rosenbrock Banana function
>     on.exit(print(f))
>     x1 <- x[1]
>     x2 <- x[2]
>     f <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
>     f
> }
> 
> fvals <- capture.output(ans <- optim(c(-1.2,1), fr))
> 
> Now, `fvals' contains character elements, but I would like to obtain the 
> actual numerical values.  How can I do this?
> 
> Thanks very much for any suggestions.
> 
> Best,
> Ravi.
> 
> -------------------------------------------------------
> Ravi Varadhan, Ph.D.
> Assistant Professor,
> Division of Geriatric Medicine and Gerontology School of Medicine Johns 
> Hopkins University
> 
> Ph. (410) 502-2619
> email: rvarad...@jhmi.edu<mailto:rvarad...@jhmi.edu>
> 
> 
>       [[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.

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

Reply via email to