How to keep the numeric part of a string in R

The text below is an output in text format. I want to retain or extract only the numeric part of a string using R. For example, I want to extract 56.1129 (as numeric) from a file containing the text below. I am reading the text line by line and would like to extract the value associated with '1 Animal'. It would be great if I could convert them to columns and extract lines 2 (Variance) and 3 (pheno1) as well.

---------------------------- -------------------------------------
           - - - Results from analysis of BBBt - - -

   1 Animal                   56.1129    
   2 Variance                 47.6055    
   3 pheno1  1                 103.72       0.92562    
Her1        = Animal     1/pheno1     3=          0.5410    0.0162
Notice: The parameter estimates are followed by their approximate standard errors.
----------------------------------------------------------------

Answers


One option is to treat this as a fixed width file and use read.fwf:

txt <- "---------------------------- -------------------------------------
           - - - Results from analysis of BBBt - - -

   1 Animal                   56.1129    
   2 Variance                 47.6055    
   3 pheno1  1                 103.72       0.92562    
Her1        = Animal     1/pheno1     3=          0.5410    0.0162
Notice: The parameter estimates are followed by their approximate standard errors.
----------------------------------------------------------------"

Read the data:

read.fwf(textConnection(txt), widths=c(4, 23, 10), skip=3, nrows=3)

The result is a data frame that you can then manipulate as you wish.

  V1                      V2       V3
1  1  Animal                  56.1129
2  2  Variance                47.6055
3  3  pheno1  1              103.7200

Need Your Help

How can I monitor a folder for changes in the background of a Windows App?

c# visual-studio windows-store-apps

I am writing a Windows 8 App that will primarily run in the background. This app will be specific to computers (i.e. no tablets or phones, in case that makes a difference). It needs to be able to...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.