[R] How can I avoid a for-loop through sapply or lapply ?

(Ted Harding) Ted.Harding at manchester.ac.uk
Tue Sep 29 18:25:03 CEST 2009


On 29-Sep-09 16:03:31, mauede at alice.it wrote:
> Through converting a miRNAs file from FASTA to character  format I get
> a vector which looks like the following:
> 
>> nml
>   [1] "hsa-let-7a MIMAT0000062 Homo sapiens let-7a"            
>   [2] "hsa-let-7b MIMAT0000063 Homo sapiens let-7b"            
>   [3] "hsa-let-7c MIMAT0000064 Homo sapiens let-7c"            
>   [4] "hsa-let-7d MIMAT0000065 Homo sapiens let-7d"            
>   [5] "hsa-let-7e MIMAT0000066 Homo sapiens let-7e"            
>   [6] "hsa-let-7f MIMAT0000067 Homo sapiens let-7f"            
>   [7] "hsa-miR-15a MIMAT0000068 Homo sapiens miR-15a"          
>   [8] "hsa-miR-16 MIMAT0000069 Homo sapiens miR-16"            
>   [9] "hsa-miR-17 MIMAT0000070 Homo sapiens miR-17"            
>  [10] "hsa-miR-18a MIMAT0000072 Homo sapiens miR-18a"   
>        
> ........................................................................
> ...............................
> [888] "hsa-miR-675* MIMAT0006790 Homo sapiens miR-675*"        
> [889] "hsa-miR-888* MIMAT0004917 Homo sapiens miR-888*"        
> [890] "hsa-miR-541* MIMAT0004919 Homo sapiens miR-541*" 
> 
> 
> My goal is to separate into a vector only the first string preceding
> the 1st space starting from the left.
> With reference to the records above listed I would obtain:
>  [1] "hsa-let-7a"            
>   [2] "hsa-let-7b"            
>   [3] "hsa-let-7c"            
>   [4] "hsa-let-7d"            
>   [5] "hsa-let-7e"            
>   [6] "hsa-let-7f f"            
>   [7] "hsa-miR-15a"          
>   [8] "hsa-miR-16"            
>   [9] "hsa-miR-17"            
>  [10] "hsa-miR-18a"   
>        
> ........................................................................
> ...............................
> [888] "hsa-miR-675*"        
> [889] "hsa-miR-888*"        
> [890] "hsa-miR-541*"
> [...]

Hi Maura,
Example:

  Strings<-c(
    "hsa-let-7a MIMAT0000062 Homo sapiens let-7a",
    "hsa-let-7b MIMAT0000063 Homo sapiens let-7b",
    "hsa-let-7c MIMAT0000064 Homo sapiens let-7c")

  sub(" .*","",Strings)
  # [1] "hsa-let-7a" "hsa-let-7b" "hsa-let-7c"

Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 29-Sep-09                                       Time: 17:25:00
------------------------------ XFMail ------------------------------




More information about the R-help mailing list