Hi
I wonder if there's a smarter way to do this procedure.
I have a vector of filenames wher I only am interested in the first part of
the filename.
I would use the following method of extracting the first part. But is there
a more simple way of doing this?
Names <-
c("A1-F1.txt","A2-F1.txt","AB1-F1.txt","A1-F2.txt","AB1-F2.txt","B1-F2.txt")
#..
temp <- strsplit(Names,"-")
temp <- unlist(temp)
temp <- temp[seq(from=1,to=2*length(Names),by=2)]
> temp
[1] "A1" "A2" "AB1" "A1"
"AB1" "B1"
Thanks for any feedback.
Regards Tom
--
View this message in context:
http://www.nabble.com/Extracting-only-one-part-of-an-string-tf4833641.html#a13828611
Sent from the R help mailing list archive at Nabble.com.
I'd use: sapply(strsplit(Names, "-"), "[[", 1) b On Nov 19, 2007, at 2:14 AM, Tom.O wrote:> > Hi > I wonder if there's a smarter way to do this procedure. > > I have a vector of filenames wher I only am interested in the first > part of > the filename. > I would use the following method of extracting the first part. But > is there > a more simple way of doing this? > > Names <- > c("A1-F1.txt","A2-F1.txt","AB1-F1.txt","A1-F2.txt","AB1-F2.txt","B1- > F2.txt") > #.. > temp <- strsplit(Names,"-") > temp <- unlist(temp) > temp <- temp[seq(from=1,to=2*length(Names),by=2)] > >> temp > [1] "A1" "A2" "AB1" "A1" "AB1" "B1" > > Thanks for any feedback. > Regards Tom > -- > View this message in context: http://www.nabble.com/Extracting-only-one-part-of-an-string-tf4833641.html#a13828611 > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
Just replace the minus and everything after it with an empty string:
sub("-.*", "", Names)
On Nov 19, 2007 2:14 AM, Tom.O <tom.olsson at dnbnor.com>
wrote:>
> Hi
> I wonder if there's a smarter way to do this procedure.
>
> I have a vector of filenames wher I only am interested in the first part
of
> the filename.
> I would use the following method of extracting the first part. But is there
> a more simple way of doing this?
>
> Names <-
>
c("A1-F1.txt","A2-F1.txt","AB1-F1.txt","A1-F2.txt","AB1-F2.txt","B1-F2.txt")
> #..
> temp <- strsplit(Names,"-")
> temp <- unlist(temp)
> temp <- temp[seq(from=1,to=2*length(Names),by=2)]
>
> > temp
> [1] "A1" "A2" "AB1" "A1"
"AB1" "B1"
>
> Thanks for any feedback.
> Regards Tom
> --
> View this message in context:
http://www.nabble.com/Extracting-only-one-part-of-an-string-tf4833641.html#a13828611
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>