[R] Extract Element of String with R's Regex

Simon Blomberg s.blomberg1 at uq.edu.au
Fri Aug 1 08:48:23 CEST 2008


How about:

unlist(strsplit(x, split=" "))[c(4:5,10)]

That perl script looks like a good reason to avoid perl.

Simon.

On Fri, 2008-08-01 at 15:13 +0900, Edward Wijaya wrote:
> Hi,
> 
> I have this string, in which I want to extract some of it's element:
> 
> > x <- "Best-K Gene 11340 211952_at RANBP5  Noc= 3 - 2  LL= -963.669 -965.35"
> 
> yielding this array
> 
> [1] "211952_at"  "RANBP5" "2"
> 
> 
> 
> In Perl we would do it this way:
> 
> __BEGIN__
> my @needed =();
> my $str = "Best-K Gene 11340 211952_at RANBP5  Noc= 3 - 2  LL=
> -963.669 -965.35";
> $str =~ /Best-K Gene \d+ (\w+) (\w+) Noc= \d - (\d) LL= (.*)/;
> push @needed, ($1,$2,$3);
> __END___
> 
> How can we achieve this with R?
> 
>  - E.W.
> 
> ______________________________________________
> R-help at 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.
-- 
Simon Blomberg, BSc (Hons), PhD, MAppStat. 
Lecturer and Consultant Statistician 
Faculty of Biological and Chemical Sciences 
The University of Queensland 
St. Lucia Queensland 4072 
Australia
Room 320 Goddard Building (8)
T: +61 7 3365 2506
http://www.uq.edu.au/~uqsblomb
email: S.Blomberg1_at_uq.edu.au

Policies:
1.  I will NOT analyse your data for you.
2.  Your deadline is your problem.

The combination of some data and an aching desire for 
an answer does not ensure that a reasonable answer can 
be extracted from a given body of data. - John Tukey.



More information about the R-help mailing list