David Romano
2012-Sep-07 18:12 UTC
[R] splitting character vectors into multiple vectors using strsplit
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
Duncan Murdoch
2012-Sep-07 18:20 UTC
[R] splitting character vectors into multiple vectors using strsplit
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"
arun
2012-Sep-07 19:05 UTC
[R] splitting character vectors into multiple vectors using strsplit
Hi, You can also try this: unlist(lapply(splitlist,head,1)) #[1] "a1" "a2" ?unlist(lapply(splitlist,tail,1)) #[1] "b1" "b2" A.K. ________________________________ From: David Romano <romanod at grinnell.edu> To: r-help at r-project.org Sent: Friday, September 7, 2012 2:12 PM Subject: [R] splitting character vectors into multiple vectors using strsplit 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 ______________________________________________ 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.
arun
2012-Sep-07 20:22 UTC
[R] splitting character vectors into multiple vectors using strsplit
Hi, You can also try this: ?sapply(lapply(splitlist,rle),`[[`,2)[1,] #[1] "a1" "a2" ?sapply(lapply(splitlist,rle),`[[`,2)[2,] #[1] "b1" "b2" A.K. ----- Original Message ----- From: David Romano <romanod at grinnell.edu> To: r-help at r-project.org Cc: Sent: Friday, September 7, 2012 2:12 PM Subject: [R] splitting character vectors into multiple vectors using strsplit 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 ______________________________________________ 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.
David Romano
2012-Nov-02 19:49 UTC
[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@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. Michael Weylandt
2012-Nov-02 20:02 UTC
[R] splitting character vectors into multiple vectors using strsplit
On Fri, Nov 2, 2012 at 7:49 PM, David Romano <romanod at grinnell.edu> wrote:> 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)Indeed it does, called, not surprisingly, "identity." You managed to guess the definition ;-) ?force also does the same, but the reason for the name is perhaps a lesson for another time. Also, note that in certain circumstances c(x) != x because c() strips attributes. Cheers, Michael
arun
2012-Nov-02 20:14 UTC
[R] splitting character vectors into multiple vectors using strsplit
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.