x <- "1041281__2009_08_20_.lev" I would like to split this string up and only extract the leading numbers. 1041281 to use as a label for a data column in a bigger for loop function to read in data. regards, -- Stephen Sefick Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis
> x <- "1041281__2009_08_20_.lev" > strsplit(x, '_')[[1]][1][1] "1041281"> -----Original Message----- > From: r-help-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of stephen sefick > Sent: Friday, August 21, 2009 3:51 PM > To: r-help at r-project.org > Subject: [R] splitting a string up > > x <- "1041281__2009_08_20_.lev" > > I would like to split this string up and only extract the > leading numbers. > > 1041281 > > to use as a label for a data column in a bigger for loop > function to read in data. > regards, > > -- > Stephen Sefick > > Let's not spend our time and resources thinking about things > that are so little or so large that all they really do for us > is puff us up and make us feel like gods. We are mammals, > and have not exhausted the annoying little problems of being mammals. > > > -K. Mullis > > ______________________________________________ > 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. >
gsub("\\_.*","",x)
This assume "_" if the first character following the numbers. You may
need a
character class if it can be one of several.
?gsub
?regex
for further (terse for the latter) details.
Bert Gunter
Genentech Nonclinical Biostatisics
-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
On
Behalf Of stephen sefick
Sent: Friday, August 21, 2009 12:51 PM
To: r-help at r-project.org
Subject: [R] splitting a string up
x <- "1041281__2009_08_20_.lev"
I would like to split this string up and only extract the leading numbers.
1041281
to use as a label for a data column in a bigger for loop function to
read in data.
regards,
--
Stephen Sefick
Let's not spend our time and resources thinking about things that are
so little or so large that all they really do for us is puff us up and
make us feel like gods. We are mammals, and have not exhausted the
annoying little problems of being mammals.
-K. Mullis
______________________________________________
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.
Try this:
gsub("__.*", "", "1041281__2009_08_20_.lev")
On Fri, Aug 21, 2009 at 4:50 PM, stephen sefick <ssefick@gmail.com> wrote:
> x <- "1041281__2009_08_20_.lev"
>
> I would like to split this string up and only extract the leading numbers.
>
> 1041281
>
> to use as a label for a data column in a bigger for loop function to
> read in data.
> regards,
>
> --
> Stephen Sefick
>
> Let's not spend our time and resources thinking about things that are
> so little or so large that all they really do for us is puff us up and
> make us feel like gods. We are mammals, and have not exhausted the
> annoying little problems of being mammals.
>
> -K. Mullis
>
> ______________________________________________
> 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.
>
--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O
[[alternative HTML version deleted]]
On Aug 21, 2009, at 2:50 PM, stephen sefick wrote:> x <- "1041281__2009_08_20_.lev" > > I would like to split this string up and only extract the leading > numbers. > > 1041281 > > to use as a label for a data column in a bigger for loop function to > read in data. > regards,At least four options: > gsub("_.*", "", x) [1] "1041281" > gsub("^([0-9]*)_.*", "\\1", x) [1] "1041281" > sapply(strsplit(x, split = "_"), "[", 1) [1] "1041281" > substr(x, 1, 7) [1] "1041281" The fourth example presumes that the initial numeric sequence is always 7 characters in length. The first three do not make that presumption. All will work where 'x' might contain multiple entries of a similar configuration as 'x'. See ?gsub, ?regex, ?strsplit and ?substr for more information. HTH, Marc Schwartz
strsplit("1041281__2009_08_20_.lev", split="_")[[1]][1]
HTH,
Stephan
stephen sefick schrieb:> x <- "1041281__2009_08_20_.lev"
>
> I would like to split this string up and only extract the leading numbers.
>
> 1041281
>
> to use as a label for a data column in a bigger for loop function to
> read in data.
> regards,
>