[R] splitting character vectors into multiple vectors using strsplit

Duncan Murdoch murdoch.duncan at gmail.com
Fri Sep 7 20:20:14 CEST 2012


On 07/09/2012 2:12 PM, David Romano wrote:
> Hi folks,
>
> Suppose I create the character vector charvec by
>
> > charvec<-c("a1.b1","a2.b2")
> > charvec
> [1] "a1.b1" "a2.b2"
>
> and then I use strsplit on charvec as follows:
>
> > splitlist<-strsplit(charvec,split=".",fixed=TRUE)
> > splitlist
> [[1]]
> [1] "a1" "b1"
>
> [[2]]
> [1] "a2" "b2"
>
>
> I was wondering whether there is already a function which can extract
> the "a" and "b" parts of the list splitlist; that is, that can return
> the same vectors as those created by c("a1","a2") and c("b1","b2").
>

sapply is the one that comes to mind:

 > sapply(splitlist, "[[", 1)
[1] "a1" "a2"
 > sapply(splitlist, "[[", 2)
[1] "b1" "b2"




More information about the R-help mailing list