Skip to content
Prev 263654 / 398502 Next

How to capture console output in a numeric format

On Fri, Jun 24, 2011 at 11:10 AM, Ravi Varadhan <rvaradhan at jhmi.edu> wrote:
This works in general assuming that we only wish to keep those output
lines having the same number of numerics as the most frequently output
length, i.e. 3 in your example above and 1 in the original example.
Also it does not require modification of the function to be optimized.

The first line extracts all numerics that have a decimal in them (so
that, for example, the 1 in [1] is not extracted).  The next two lines
count how many numbers are in each extracted line and returns the most
frequently occurring non-zero length as n so that n is 3 here.  The
final line extracts those lines and rbind's them. fvals is from your
post.

library(gsubfn)
fvals.numeric <- strapply(fvals, "[-0-9]+[.][0-9]*", as.numeric)
tab <- table(sapply(fvals.numeric, length))
n <- as.numeric(names(which.max(tab[names(tab) > 0])))
do.call(rbind, Filter(function(x) length(x) == n, fvals.numeric))

If we knew in advance that we wanted only lines with 3 numbers and
that those three numbers are each prefaced by a space (both of which
are the case in your example) then we could simplify it to just this:

library(gsubfn)
fvals.numeric <- strapply(fvals, " [-0-9.]+", as.numeric)
do.call(rbind, fn$Filter(x ~ length(x) == 3, fvals.numeric))

There is more on strapply and fn$ at http://gsubfn.googlecode.com