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

Marc Schwartz (via MN) mschwartz at mn.rr.com
Mon Sep 25 18:37:44 CEST 2006


On Mon, 2006-09-25 at 11:30 -0500, Marc Schwartz (via MN) wrote:
> On Mon, 2006-09-25 at 11:04 -0500, Frank Duan 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
> 
> 
> Something like this using gsub() should work I think:
> 
> > DF
>       V1
> 1 123abc
> 2 12cd34
> 3   1e23
> 
> 
> # Replace letters and any following chars with ""
> DF$V2 <- gsub("[A-Za-Z]+.*", "", DF$V1)

Quick typo correction here. It should be:

DF$V2 <- gsub("[A-Za-z]+.*", "", DF$V1)

The second 'z' should be lower case.


Marc



More information about the R-help mailing list