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)


# Replace any initial numbers with ""
DF$V3 <- gsub("^[0-9]+", "", DF$V1)


> DF
      V1  V2   V3
1 123abc 123  abc
2 12cd34  12 cd34
3   1e23   1  e23

See ?gsub and ?regex for more information.

HTH,

Marc Schwartz

______________________________________________
[email protected] 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.

Reply via email to