Re: [R] Splitting a character variable into a numeric one and a character one?

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Mon 25 Sep 2006 - 17:00:45 GMT

And here is a third solution not using package gsubfn:

s <- c("123abc", "12cd34", "1e23")
out <- gsub("^(([[:digit:]]+)(.*))", "\\1 \\2 \\3", s) read.table(textConnection(out), as.is = TRUE)

Again, if spaces appear in the input string choose a character not appearing, such as comma, and do it like this:

s <- c("123abc", "12cd34", "1e23")
out <- gsub("^(([[:digit:]]+)(.*))", "\\1,\\2,\\3", s) read.table(textConnection(out), sep = ",", as.is = TRUE)

On 9/25/06, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
> Here is one more solution:
>
> library(gsubfn)
> s <- c("123abc", "12cd34", "1e23")
>
> out <- gsubfn("^([[:digit:]]+)(.*)", paste, s, backref = -2)
> read.table(textConnection(out))
>
> It assumes there are no spaces in the strings. If
> there are then choose a sep= that does not appear
> and do this:
>
> sep = ","
> f <- function(x, y) paste(x, y, sep = sep)
> out <- gsubfn("^([[:digit:]]+)(.*)", f, s, backref = -2)
> read.table(textConnection(out), sep = sep)
>
>
> On 9/25/06, Gabor Grothendieck <ggrothendieck@gmail.com> wrote:
> > strapply in package gsubfn can do that:
> >
> >
> > library(gsubfn)
> > s <- c("123abc", "12cd34", "1e23")
> >
> > out <- strapply(s, "^([[:digit:]]+)(.*)", c)
> > out <- do.call(rbind, out) # as a matrix
> >
> > data.frame(x = out[,1], num = as.numeric(out[,2]), char = out[,3]) #
> > as a data.frame
> >
> >
> > On 9/25/06, Frank Duan <fhduan@gmail.com> wrote:
> > > Hi All,
> > >
> > > I have a data with a variable like this:
> > >
> > > Column 1
> > >
> > > "123abc"
> > > "12cd34"
> > > "1e23"
> > > ...
> > >
> > > Now I want to do an operation that can split it into two variables:
> > >
> > > Column 1 Column 2 Column 3
> > >
> > > "123abc" 123 "abc"
> > > "12cd34" 12 "cd34"
> > > "1e23" 1 "e23"
> > > ...
> > >
> > > So basically, I want to split the original variabe into a numeric one and a
> > > character one, while the splitting element is the first character in Column
> > > 1.
> > >
> > > I searched the forum with key words "strsplit"and "substr", but still can't
> > > solve this problem. Can anyone give me some hints?
> > >
> > > Thanks in advance,
> > >
> > > FD
> > >
> > > [[alternative HTML version deleted]]
> > >
> > > ______________________________________________
> > > R-help@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
> > > and provide commented, minimal, self-contained, reproducible code.
> > >
> >
>



R-help@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 and provide commented, minimal, self-contained, reproducible code. Received on Tue Sep 26 03:03:02 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 25 Sep 2006 - 17:30:18 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.