I run R 2.1.1 in a Linux environment (RedHat 9) although my question
is not platform-specific.
Consider the following:
> A <- c("Prefix-aaa", "Prefix-bbb",
"Prefix-ccc")
> B <- strsplit(A, "-")
> B
[[1]]
[1] "Prefix" "aaa"
[[2]]
[1] "Prefix" "bbb"
[[3]]
[1] "Prefix" "ccc"
How do I extract the elements "aaa", "bbb", "ccc"
from B?
Thanks in advance.
Dennis Fisher MD
P < (The "P Less Than" Company)
Phone: 1-866-PLessThan (1-866-753-7784)
Fax: 1-415-564-2220
www.PLessThan.com
[[alternative HTML version deleted]]
Dennis Fisher wrote:> I run R 2.1.1 in a Linux environment (RedHat 9) although my question > is not platform-specific. > > Consider the following: > > A <- c("Prefix-aaa", "Prefix-bbb", "Prefix-ccc") > > B <- strsplit(A, "-") > > B > [[1]] > [1] "Prefix" "aaa" > > [[2]] > [1] "Prefix" "bbb" > > [[3]] > [1] "Prefix" "ccc" > > How do I extract the elements "aaa", "bbb", "ccc" from B?For example: sapply(B, "[", 2) Uwe Ligges> Thanks in advance. > > Dennis Fisher MD > P < (The "P Less Than" Company) > Phone: 1-866-PLessThan (1-866-753-7784) > Fax: 1-415-564-2220 > www.PLessThan.com > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
A <- c("Prefix-aaa", "Prefix-bbb",
"Prefix-ccc")
sapply( strsplit(A, split="-"), function(x) x[2] )
[1] "aaa" "bbb" "ccc"
Regards, Adai
On Fri, 2005-07-22 at 03:45 -0700, Dennis Fisher wrote:> I run R 2.1.1 in a Linux environment (RedHat 9) although my question
> is not platform-specific.
>
> Consider the following:
> > A <- c("Prefix-aaa", "Prefix-bbb",
"Prefix-ccc")
> > B <- strsplit(A, "-")
> > B
> [[1]]
> [1] "Prefix" "aaa"
>
> [[2]]
> [1] "Prefix" "bbb"
>
> [[3]]
> [1] "Prefix" "ccc"
>
> How do I extract the elements "aaa", "bbb",
"ccc" from B?
>
> Thanks in advance.
>
> Dennis Fisher MD
> P < (The "P Less Than" Company)
> Phone: 1-866-PLessThan (1-866-753-7784)
> Fax: 1-415-564-2220
> www.PLessThan.com
>
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
>
There have already been some good solutions but here are three
others just to see a range of approaches:
sub("^[^-]*-", "", A) # remove everything up to first minus
sub("Prefix-", "", A) # simplified version if prefix known
substring(A, 8) # simplification if prefix always 7 chars including minus
> On Fri, 2005-07-22 at 03:45 -0700, Dennis Fisher wrote:
> > I run R 2.1.1 in a Linux environment (RedHat 9) although my question
> > is not platform-specific.
> >
> > Consider the following:
> > > A <- c("Prefix-aaa", "Prefix-bbb",
"Prefix-ccc")
> > > B <- strsplit(A, "-")
> > > B
> > [[1]]
> > [1] "Prefix" "aaa"
> >
> > [[2]]
> > [1] "Prefix" "bbb"
> >
> > [[3]]
> > [1] "Prefix" "ccc"
> >
> > How do I extract the elements "aaa", "bbb",
"ccc" from B?