[R] get the first character element from a list

David Winsemius dwinsemius at comcast.net
Fri Feb 10 00:04:59 CET 2012


On Feb 9, 2012, at 5:01 PM, Changbin Du wrote:

> test<- 
> c("20120111_181515_001_CCL54D_A01_S02_APL932_PL11_DL_20120111.CEL",
> "20120111_181516_002_CCL54D_A02_S08_APL932_PL11_DL_20120111.CEL")
>
>> test
> [1] "20120111_181515_001_CCL54D_A01_S02_APL932_PL11_DL_20120111.CEL"
> [2] "20120111_181516_002_CCL54D_A02_S08_APL932_PL11_DL_20120111.CEL"
>
> fields1<-strsplit(test, "_")
>
>> fields1
> [[1]]
> [1] "20120111"     "181515"       "001"          "CCL54D"
> "A01"
> [6] "S02"          "APL932"       "PL11"         "DL"
> "20120111.CEL"
>
> [[2]]
> [1] "20120111"     "181516"       "002"          "CCL54D"
> "A02"
> [6] "S08"          "APL932"       "PL11"         "DL"
> "20120111.CEL"
>
>> *releaseqc<-sapply(fields1, "[",4)*
>
> releaseqc
> [1] "CCL54D" "CCL54D"
>
> _____________
> Dear R community,
>
> I used the *releaseqc<-sapply(fields1, "[",4)* to get the 4th  
> elements from
> each sub-list within fields1 list.
>
> How do I get the first non-digital or first character element from  
> each
> list?

Perhaps something like:

  mapply ( "[", fields1, sapply(fields1, function(x)  
{ which.min( is.na( is.numeric(fields1))) }

There are certainly other  grep()-ish ways of approaching this.

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list