[R] splitting character vectors into multiple vectors using strsplit

arun smartpink111 at yahoo.com
Fri Nov 2 21:14:31 CET 2012


HI,
You can also use ?abind() in this case:
library(abind)
abind(splitlist,along=2)
 #    [,1] [,2]
#[1,] "a1" "a2"
#[2,] "b1" "b2"
A.K.




----- Original Message -----
From: David Romano <romanod at grinnell.edu>
To: r-help at r-project.org
Cc: 
Sent: Friday, November 2, 2012 3:49 PM
Subject: Re: [R] splitting character vectors into multiple vectors using strsplit

Hi again,

I just wanted to thank folks for their suggestions; they led me to
understand the *apply family a little better, and to realize the issue was
really a question of how to convert a list of equal length vectors into a
matrix.  In this case sapply only needs to be asked to identify these
vectors individually; I don't know if R has the equivalent of an identity
function, but the following solution accomplishes this:

> splitvectors <- sapply(splitlist, function(x) x)
> splitvectors
     [,1] [,2]
[1,] "a1" "a2"
[2,] "b1" "b2"

or, by replacing the anonymous function by c, we obtain a more elegant but
more wasteful solution.

Thanks again for everyone's help,
David Romano

On Fri, Sep 7, 2012 at 11:12 AM, David Romano <romanod at grinnell.edu> 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").
>
> Thanks,
> David Romano
>

    [[alternative HTML version deleted]]

______________________________________________
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.





More information about the R-help mailing list