Greetings!
For some reason I am not managing to work out how to do this
(in principle) simple task!
As a result of applying strsplit() to a vector of character strings,
I have a long list L (N elements), where each element is a vector
of two character strings, like:
L[1] = c("A1","B1")
L[2] = c("A2","B2")
L[3] = c("A3","B3")
[etc.]
>From L, I wish to obtain (as directly as possible, e.g. avoiding
a loop) two vectors each of length N where one contains the strings
that are first in the pair, and the other contains the strings
which are second, i.e. from L (as above) I would want to extract:
V1 = c("A1","A2","A3",...)
V2 = c("B1","B2","B3",...)
Suggestions?
With thanks,
Ted.
-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 25-Apr-2013 Time: 11:16:46
This message was sent by XFMail
Dear Dr. Harding, Try sapply(L, "[", 1) sapply(L, "[", 2) HTH, Jorge.- On Thu, Apr 25, 2013 at 8:16 PM, Ted Harding <Ted.Harding@wlandres.net>wrote:> Greetings! > For some reason I am not managing to work out how to do this > (in principle) simple task! > > As a result of applying strsplit() to a vector of character strings, > I have a long list L (N elements), where each element is a vector > of two character strings, like: > > L[1] = c("A1","B1") > L[2] = c("A2","B2") > L[3] = c("A3","B3") > [etc.] > > >From L, I wish to obtain (as directly as possible, e.g. avoiding > a loop) two vectors each of length N where one contains the strings > that are first in the pair, and the other contains the strings > which are second, i.e. from L (as above) I would want to extract: > > V1 = c("A1","A2","A3",...) > V2 = c("B1","B2","B3",...) > > Suggestions? > > With thanks, > Ted. > > ------------------------------------------------- > E-Mail: (Ted Harding) <Ted.Harding@wlandres.net> > Date: 25-Apr-2013 Time: 11:16:46 > This message was sent by XFMail > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Hi,
May be this helps.
L<-
list(c("A1","B1"),c("A2","B2"),c("A3","B3"))
simplify2array(L)[1,]
#[1] "A1" "A2" "A3"
simplify2array(L)[2,]
#[1] "B1" "B2" "B3"
#or
library(stringr)
?word(sapply(L,paste,collapse=" "),1)
#[1] "A1" "A2" "A3"
A.K.
----- Original Message -----
From: "Ted.Harding at wlandres.net" <Ted.Harding at
wlandres.net>
To: r-help at r-project.org
Cc:
Sent: Thursday, April 25, 2013 6:16 AM
Subject: [R] Decomposing a List
Greetings!
For some reason I am not managing to work out how to do this
(in principle) simple task!
As a result of applying strsplit() to a vector of character strings,
I have a long list L (N elements), where each element is a vector
of two character strings, like:
? L[1] = c("A1","B1")
? L[2] = c("A2","B2")
? L[3] = c("A3","B3")
? [etc.]
>From L, I wish to obtain (as directly as possible, e.g. avoiding
a loop) two vectors each of length N where one contains the strings
that are first in the pair, and the other contains the strings
which are second, i.e. from L (as above) I would want to extract:
? V1 = c("A1","A2","A3",...)
? V2 = c("B1","B2","B3",...)
Suggestions?
With thanks,
Ted.
-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 25-Apr-2013? Time: 11:16:46
This message was sent by XFMail
______________________________________________
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.